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 Input3 3
1 2
2 1
2 3Sample Output1HINT
100% of Data n<=10000,m<=50000
Source
1 A la la la-New Posture Tarjan algorithm (now too weak!) ), the Tarjan algorithm after the strong connected components to shrink points, and then find the degree of 0 of the only point (the point of contraction), why is unique, very simple, because the other points have a degree, that is, all the other points are linked together point to the point, if not unique, there is no solution.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <stack>6 #defineN 100107 #defineM 500508 using namespacestd;9 structdata1{intP,next;} E[M];Ten intANS,CNT,N,M,SCC; One intHead[n],dfn[n],low[n],vis[n],inq[n],h[n],belong[n],ringsum[n]; Astack<int>Q; - voidSeintXintY) {cnt++;e[cnt].next=head[x];head[x]=cnt;e[cnt].p=y;} - voidTarjan (intx) the { -vis[x]=inq[x]=1; -low[x]=dfn[x]=++CNT;; - Q.push (x); + for(inti=head[x];i!=-1; i=e[i].next) - { + if(!VIS[E[I].P]) A { at Tarjan (E[I].P); -low[x]=min (low[x],low[e[i].p]); - } - Else if(INQ[E[I].P]) low[x]=min (low[x],low[e[i].p]); - } - if(dfn[x]==Low[x]) in { - intNow ; toScc++; + while(now!=x) - { thenow=q.top (); Q.pop (); *belong[now]=SCC; $inq[now]=0;Panax Notoginseng++RINGSUM[SCC]; - } the } + } A voidPart1_tarjan () the { +Cnt=0; - for(intI=1; i<=n;i++) $ if(!Vis[i]) $ Tarjan (i); - } - voidPart2_shr_point () the { - for(intI=1; i<=n;i++)Wuyi for(intt=head[i];t!=-1; t=e[t].next) the if(belong[i]!=BELONG[E[T].P]) -h[belong[i]]=1; Wu } - voidPart3_doit () About { $ for(intI=1; i<=scc;i++) - if(!H[i]) - { - if(ANS) A { +ans=0; the return; - } $ Elseans=Ringsum[i]; the } the } the intMain () the { -memset (e,-1,sizeof(e)); inmemset (head,-1,sizeof(head)); thescanf"%d%d",&n,&m); the for(intI=1; i<=m;i++) About { the intx, y; thescanf"%d%d",&x,&y); the se (x, y); + } - Part1_tarjan (); the part2_shr_point ();Bayi Part3_doit (); theprintf"%d\n", ans); the return 0; -}
View Code
"BZOJ1051" 1051: [HAOI2006] Popular bull Tarjan strong connected component + Pinch point