Codeforces Round #261 (Div. 2) E. Pashmak and Graph (sorting + dp)
来源:互联网 发布:云南大学网络平台 编辑:程序博客网 时间:2024/06/05 21:16
time limit per test
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.
You are given a weighted directed graph with n vertices andm edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.
Help Pashmak, print the number of edges in the required path.
The first line contains two integers n,m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then,m lines follows. The i-th line contains three space separated integers: ui, vi, wi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertexui to vertexvi.
It's guaranteed that the graph doesn't contain self-loops and multiple edges.
Print a single integer — the answer to the problem.
3 31 2 12 3 13 1 1
1
3 31 2 12 3 23 1 3
3
6 71 2 13 2 52 4 22 5 22 6 95 4 34 3 4
6
In the first sample the maximum trail can be any of this trails: .
In the second sample the maximum trail is .
In the third sample the maximum trail is .
解析:先将所有边按权值从小到大排序,然后再按权值递增dp。
AC代码:
#include <bits/stdc++.h>using namespace std;const int N = 300005;struct edge{ int x, y, w; void in(){ scanf("%d%d%d", &x, &y, &w); } bool operator <(const edge &a) const{ return w < a.w; }} E[N];int dp[N];int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk int n, m; while(scanf("%d%d", &n, &m)!=EOF){ memset(dp, 0, sizeof(dp)); for(int i=0; i<m; i++) E[i].in(); sort(E, E + m); int ans = 0; int cur = E[0].w; for(int i=0; i<m; ){ vector<pair<int, int> > p; while(E[i].w == cur){ int foo = max(dp[ E[i].y ], dp[ E[i].x ] + 1); ans = max(ans, foo); p.push_back(make_pair(E[i].y, foo)); i ++; } for(int j=0; j<p.size(); j++){ dp[ p[j].first ] = max(dp[ p[j].first ], p[j].second); } cur = E[i].w; } printf("%d\n", ans); } return 0;}
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph (sorting + dp)
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph【DP】
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round #261 (Div. 2)——Pashmak and Graph
- Codeforces Round #261(Div 2) E Pashmak and Graph(图中严格递增的最长路径、思维)
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers (sorting)
- Codeforces 459E Pashmak and Graph【Dp】
- Codeforces 459E. Pashmak and Graph (DP)
- codeforces 459E E. Pashmak and Graph(dp)
- codeforces 459E E. Pashmak and Graph(dp)
- Codeforces 459E Pashmak and Graph(dp+贪心)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden (水题)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden
- Codeforces Round #261 (Div. 2)B. Pashmak and Flowers
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- Lucene:基于Java的全文检索引擎简介
- Centos 6.2 安装Mysql笔记
- SlidingMenu使用(二) Demo
- 提高篇第28-31课第三题
- org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Server returned error code = 40
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph (sorting + dp)
- codeforces Round#158 Div.2 C
- Hihocoder 顺子
- 黑马程序员JAVA笔记1--java基础和概述
- adroid 杂项
- win7 64位pl/sql配置
- 组网
- Haskell之Yesod开发–简单网站开发(五)
- 15.8