Description
the desire of every cow is to become one of the most popular cows. Now there are n cows, give you m-pairs of integers (a, a, b), indicating that cow A is considered to be popular. This relationship is transitive, and if a thinks B is popular, B thinks C is popular, then bull a also thinks that Ox C is popular. Your task is to find out how many cows are considered popular by all cows.
Input
The first line is two numbers n,m. Next m line, two numbers per line, A, B, meaning that A is considered to be welcome (the information given may be repeated, that is, there may be multiple a , b)
Output
a number, that is, how many cows are considered popular by all cows.
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1 "Data range"
10% of Data n<=20, m<=50
30% of data n<=1000,m<=20000
70% of data n<=5000,m<=50000
100% of data n<=10000,m<=50000
Analysis: Tarjan strong connected components for the reduction of the weight of the composition, the point of 0, if only one output it represents the size of the strong connected components, otherwise no solution. Because obviously if there are two points with a degree of 0, two points cannot be mutually welcome. Template title.
#include <iostream>#include<cstdio>#include<cstdlib>using namespacestd;intn,m;inttot=0, cnt=0, top=0;structnode{intTo,next;} e[50001],d[50001];inthead[10001];intdfn[10001],low[10001],q[10001];intScc=0, h[10001],belong[10001],hav[10001];//SCC is the number of strongly connected componentsBOOLvis[10001],inq[10001];intans=0;voidDfsinta) { intNow ; Inq[a]=vis[a]=1; Low[a]=dfn[a]=++CNT; q[++top]=A; intC=Head[a]; while(c) {if(!Vis[e[c].to]) {DFS (e[c].to); Low[a]=min (low[a],low[e[c].to]); } Else if(Inq[e[c].to]) low[a]=min (low[a],dfn[e[c].to]); C=E[c].next; } if(low[a]==Dfn[a]) {SCC++; while(now!=a) { now=q[top--]; Inq[now]=0; Belong[now]=SCC; ++HAV[SCC]; } }} voidrebuild () {CNT=0; for(intI=1; i<=n; i++) { intC=Head[i]; while(c) {if(belong[e[c].to]!=Belong[i]) {d[++cnt].to=Belong[e[c].to]; D[cnt].next=H[i]; H[i]=CNT; } C=D[c].next; } }}voidWork () { for(intI=1; i<=scc; i++) if(!H[i]) { if(ans) {ans=0;return; } Elseans=Hav[i]; if(i==1) cout << hav[i]<<Endl; } }voidTarjan () { for(intI=1; i<=n; i++)if(!Vis[i]) DFS (i); Rebuild ();}intMain () {CIN>> N >>m; for(intI=1; i<=m; i++) { intx, Y, Z CIN>> x >>y; e[++tot].to=y; E[TOT].NEXT=HEAD[X]; head[x]=tot; } Tarjan (); Work (); cout<<ans; System ("Pause"); return 0;}
bzoj1051| HAOI2006 Popular Cattle | Strong connected components