CodeForces - 459E Pashmak and Graph[贪心优化dp]
2017-04-15 10:22
429 查看
E. Pashmak and Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 and m 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.
Input
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 vertex ui to vertex vi.
It's guaranteed that the graph doesn't contain self-loops and multiple edges.
Output
Print a single integer — the answer to the problem.
Examples
input
output
input
output
input
output
Note
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
.
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 and m 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.
Input
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 vertex ui to vertex vi.
It's guaranteed that the graph doesn't contain self-loops and multiple edges.
Output
Print a single integer — the answer to the problem.
Examples
input
3 3 1 2 1 2 3 1 3 1 1
output
1
input
3 3 1 2 1 2 3 2 3 1 3
output
3
input
6 7 1 2 1 3 2 5 2 4 2 2 5 2 2 6 9 5 4 3 4 3 4
output
6
Note
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,以当前边的尾节点为路径的结尾,然后一个边的首段点为u,尾端点为v, 裸地转移就是dp[v]=max(dp[v],dp[u]+1) 注意边相等要单独处理,然后就AC了…… */ #include<cstdio> #include<algorithm> #include<iostream> using namespace std; const int N=3e5+5; struct edge{int x,y,z;}e[N<<1]; int n,m;int f ,g ; bool operator <(const edge &a,const edge &b){ return a.z<b.z; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z); sort(e+1,e+m+1); for(int j,i=1;i<=m;i++){ for(j=i;e[j].z==e[i].z;j++) g[e[j].y]=max(g[e[j].y],f[e[j].x]+1); for(j=i;e[j].z==e[i].z;j++) f[e[j].y]=g[e[j].y]; i=j-1; } printf("%d\n",*max_element(f+1,f+n+1)); return 0; }
相关文章推荐
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- Codeforces 77C 树形dp + 贪心
- codeforces 372C Watching Fireworks is Fun 单调队列优化dp
- bzoj1044 木棍分割 二分+贪心&dp优化
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- Codeforces 629D Babaei and Birthday Cake(线段树优化dp)
- codeforces 154A Hometask 贪心 dp
- CodeForces 645E Intellectual Inquiry(构造+贪心+dp)
- Codeforces 543A Writing Code 【滚动数组优化dp】
- #Codeforces 323 [div2] D. Once Again 【优化dp】
- codeforces 637D D. Running with Obstacles(dp,水题,贪心)
- codeforces 271E 离散化+线段树,dp优化
- codeforces 543A A. Writing Code(完全背包优化dp )
- codeforces 543A Writing Code(dp降维优化,完全背包思想)
- Codeforces 597C Subsequences 【树状数组优化DP】
- Codeforces 459E Pashmak and Graph(dp+贪心)
- codeforces 424E Colored Jenga (状态压缩,概率dp用hash记忆优化搜索)
- codeforces 509C C. Sums of Digits(数位dp+贪心+枚举)
- Codeforces 629D Babaei and Birthday Cake 【线段树优化dp】
- Codeforces 545 C Woodcutters(贪心/DP)