E. Pashmak and Graphtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPashmak'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.
InputThe 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.
OutputPrint a single integer — the answer to the problem.
Examplesinput3 3
1 2 1
2 3 1
3 1 1output1input3 3
1 2 1
2 3 2
3 1 3output3input6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4output6NoteIn 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[edge[k].e]=max(dp[edge[k].e],dp[edge[k].s]+1).但是由于权值相同的问题,所以我们需要分段处理,把权值相同的分成一块,然后处理的时候弄一个临时的dp增加边的时候只能用前面的dp增加,因为这一块相同权值的的线段都不能放在同一个上升序列里面。
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> typedef long long ll; #define X first #define Y second #define mp(a,b) make_pair(a,b) #define pb push_back #define sd(x) scanf("%d",&(x)) #define Pi acos(-1.0) #define sf(x) scanf("%lf",&(x)) #define ss(x) scanf("%s",(x)) #define maxn 10000000 #include <ctime> const int inf=0x3f3f3f3f; const long long mod=1000000007; using namespace std; struct node { int s,e,w; }edge[300005]; int dp[300005]; int fdp[300005]; bool cmp(node a,node b) { return a.w<b.w; } int main() { #ifdef local freopen("in","r",stdin); //freopen("data.txt","w",stdout); int _time=clock(); #endif int n,m; cin>>m>>n; for(int i=0;i<n;i++)scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].w); sort(edge,edge+n,cmp); int ans=0; for(int i=0;i<n;i++) { int j; for(j=i;edge[j].w==edge[i].w&&j<n;j++); for(int k=i;k<j;k++) { fdp[edge[k].e]=max(fdp[edge[k].e],dp[edge[k].s]+1); } for(int k=i;k<j;k++) { dp[edge[k].e]=fdp[edge[k].e]; ans=max(dp[edge[k].e],ans); } i=j-1; } cout<<ans<<endl; #ifdef local printf("time: %d ",int(clock()-_time)); #endif }