Groupingtime limit:2000msmemory limit:65536kbthis problem'll be judged onZju. Original id:3795
64-bit integer IO format: %lld Java class name: Main
Suppose there N is people in Zju, whose ages is unknown. We have some messages about them. The-th message shows that the age of person is not a smaller than the age of person i si ti . Now we need to divide all these N people into several groups. One's age shouldn ' t is compared with the same group, directly or indirectly. And everyone should be assigned to one and only one group. The task is to calculate the minimum number of groups that meet the requirement.
Input
There is multiple test cases. For each test case:the first line contains-integers N (1≤ N ≤100000), M (1≤ M ≤300000), is the N num ber of people, and is the number of M messages. Then followed M by lines, each line contain the integers si and ti. There is a blank line between every and cases. Process to the end of input.
Output
For each case, print the minimum number of groups that meet the requirement one line.
Sample Input
4 41 21 32 43 4
Sample Output
3
Hint
set1= {1}, set2= {2, 3}, set3= {4}
SourceZOJ Monthly, June 2014AuthorLUO, Jiewei problem solving: Tarjan +dag the longest road of shrinking point
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100010;4 structarc{5 intTo,next;6Arcintx =0,inty =-1){7to =x;8Next =y;9 }Ten}e[500000]; One intHEAD[MAXN],DFN[MAXN],LOW[MAXN],BELONG[MAXN],NUM[MAXN]; A BOOLINSTACK[MAXN]; - intTOT,IDX,SCC,N,M,DP[MAXN]; -stack<int>Stk; thevector<int>G[MAXN]; - voidAddintUintv) { -E[tot] =arc (V,head[u]); -Head[u] = tot++; + } - voidinit () { + for(inti =0; i < MAXN; ++i) { ADp[i] = head[i] =-1; atDfn[i] = Low[i] =0; -Belong[i] = Num[i] =0; -Instack[i] =false; - g[i].clear (); - } -IDX = SCC = tot =0; in while(!stk.empty ()) Stk.pop (); - } to voidTarjan (intu) { +Dfn[u] = Low[u] = + +idx; -Instack[u] =true; the stk.push (u); * for(inti = Head[u]; ~i; i =E[i].next) { $ if(!Dfn[e[i].to]) {Panax Notoginseng Tarjan (e[i].to); -Low[u] =min (low[u],low[e[i].to]); the}Else if(Instack[e[i].to]) low[u] =min (low[u],dfn[e[i].to]); + } A if(Low[u] = =Dfn[u]) { theScc++; + intv; - Do{ $Instack[v = Stk.top ()] =false; $BELONG[V] =SCC; -num[scc]++; - Stk.pop (); the} while(V! =u); - }Wuyi } the intDfsintu) { - if(Dp[u]! =-1)returnDp[u]; WuDp[u] =Num[u]; - for(inti = g[u].size ()-1; I >=0; --i) AboutDp[u] = max (Dp[u],num[u] +DFS (G[u][i])); $ returnDp[u]; - } - intMain () { - intu,v; A while(~SCANF ("%d%d",&n,&m)) { + init (); the for(inti =0; I < m; ++i) { -scanf"%d%d",&u,&v); $ Add (u,v); the } the for(inti =1; I <= N; ++i) the if(!Dfn[i]) Tarjan (i); the for(inti =1; I <= N; ++i) { - for(intj = Head[i]; ~j; j =E[j].next) { in if(Belong[i]! =belong[e[j].to]) the G[belong[e[j].to]].push_back (Belong[i]); the } About } the intRET =0; the for(inti =1; I <= SCC; ++i) theRET =Max (Ret,dfs (i)); +printf"%d\n", ret); - } the return 0;Bayi}
View Code
ZOJ 3795 Grouping