BZOJ1051[HAOI2006] Popular Cattle
Test instructions
There are n cows, given m-pairs of integers (a, a, a, b), indicating that cow A is considered to be popular, and this relationship is transitive. Find out how many cows are considered popular by all cows. n≤10000
Exercises
Because the cattle are identified by all cattle, if the cow does not agree with any cattle, then the cow out of the degree of 0, and the degree of 0 of the cattle have and only one of the cattle will not exist. If the cow identifies other cows, then the two cows are required to identify with each other. At the same time both cows are the cattle. So do a tarjan shrinkage point, after the contraction point if the degree of 0 points there are many no cattle, if only one then this point indicates that the strong connected block all points are the cattle.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <stack>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 using namespacestd;7 8 structe{intF,t,n;}; E es[60000];intg[20000],ess;9 voidPeintFintT) {es[++ess]= (e) {f,t,g[f]}; g[f]=ess;}Ten intbel[20000],cnt[20000],n,m,sz[20000],tot,low[20000],pre[20000],tim;BOOLvis[20000],ins[20000]; OneStack <int>s; A voidDfsintx) { -vis[x]=ins[x]=1; S.push (x); low[x]=pre[x]=++Tim; - for(intI=g[x];i;i=ES[I].N) the if(!vis[es[i].t]) DFS (es[i].t), low[x]=min (low[x],low[es[i].t]); - Else if(ins[es[i].t]) low[x]=min (low[x],pre[es[i].t]); - if(low[x]==Pre[x]) { -tot++; + while(!S.empty ()) { - intNow=s.top (); S.pop (); Bel[now]=tot; sz[tot]++; ins[now]=0;if(now==x) Break; + } A } at } - voidTarjan () { - while(!s.empty ()) S.pop (); memset (Vis,0,sizeof(VIS)); memset (INS,0,sizeof(ins)); memset (SZ,0,sizeof(SZ)); -tot=tim=0; Inc (I,1, N)if(!Vis[i]) DFS (i); - } - voidsolve () { inmemset (CNT,0,sizeof(CNT)); Inc (I,1, ESS)if(bel[es[i].f]!=bel[es[i].t]) cnt[bel[es[i].f]]++;intA=0; -Inc (I,1, tot) { to if(cnt[i]==0&&a!=0) {printf ("0\n");return;} + if(cnt[i]==0) a=i; - } theprintf"%d\n", Sz[a]); * } $ intMain () {Panax Notoginsengscanf"%d%d", &n,&m); ess=0; Memset (G,0,sizeof(g)); -Inc (I,1, m) {intb; scanf"%d%d",&a,&b); PE (A, b);} the Tarjan (); solve (); + return 0; A}
20160517
BZOJ1051[HAOI2006] Popular Cattle