1051: [HAOI2006] Popular cow time limit:10 Sec Memory limit:162 MB
submit:3134 solved:1642
[Submit] [Status] [Discuss] 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 A topic that has been done a long time ago. In particular, the first Tarjan, the weight of the point is the number of points of the connected component and then the point of finding the degree of 0 only one output its weight of more than 0
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 #defineMAXN 100017 8 using namespacestd;9 TenInlineintIn () One { A intx=0;CharCh=GetChar (); - while(ch<'0'|| Ch>'9') ch=GetChar (); - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); the returnx; - } - - structed{ + intu,v; -}edge[maxn*5]; + A structnode{ at intTo,last; -}e[maxn*Ten]; - - intlast[maxn],tot=0, dfn[maxn],low[maxn],father[maxn],sta[maxn],top=0, cnt=0, size=0, NUM[MAXN],inch[MAXN]; - - BOOLINS[MAXN]; in - voidAddintUintV) {e[++tot].to=v,e[tot].last=last[u],last[u]=tot;} to + voidTarjan (intpoi) - { thedfn[poi]=low[poi]=++CNT; *ins[poi]=1; sta[++top]=poi; $ for(intI=last[poi];i;i=e[i].last)Panax Notoginseng { - intu=e[i].to; the if(!Dfn[u]) + { A Tarjan (u); thelow[poi]=min (Low[u],low[poi]); + } - Else if(Ins[u]) low[poi]=min (low[poi],dfn[u]); $ } $ if(dfn[poi]==Low[poi]) - { -size++; the intVV; - Do{Wuyivv=Sta[top]; thefather[vv]=size; -ins[vv]=0; Wunum[size]++; -top--; About} while(vv!=poi); $ } - } - - intMain () A { +Freopen ("1051.in","R", stdin); the intN,m,str=0; -N=in (), m=In (); $ for(intI=1; i<=n;i++) father[i]=i; the for(intI=1; i<=m;i++) theEdge[i].u=in (), edge[i].v=in (), add (EDGE[I].U,EDGE[I].V); the for(intI=1; i<=n;i++)if(!Dfn[i]) Tarjan (i); theMemset (Last,0,sizeof(last)); -tot=0; in for(intI=1; i<=m;i++)if(father[edge[i].u]!=FATHER[EDGE[I].V]) { the inch[father[edge[i].u]]++; the } About for(intI=1; i<=size;i++) the { the if(!inch[I]&&str) {printf ("0");return 0;} the if(!inch[i]) Str=i; + } -printf"%d", Num[str]); the return 0;Bayi}
View Code
"Tarjan" Bzoj 1051: Popular Cattle