1051: [HAOI2006] Popular bull time limit: ten Sec Memory Limit: 162 MB
Submit: 3491 Solved: 1837
[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
Source
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <algorithm> #include <stack> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #define LL long long#define maxn 10005#define maxm 50005using namespace std;struct edge{int next,to;} E[maxm],d[maxm];int N,m,cnt,scc,top,ans;int H[MAXN],S[MAXN],SZ[MAXN],DFN[MAXN],LOW[MAXN],HEAD[MAXN],BELONG[MAXN] ; inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void dfs (int x) {int tmp=0;low[x]=dfn[x]=++cnt;s[++top]=x;for (int i=head[x];i;i=e[i].next) {int y=e[i].to;if (! Dfn[y]) {dfs (y); Low[x]=min (Low[x],low[y]);} else if (!belong[y]) low[x]=min (Low[x],dfn[y]);} if (Low[x]==dfn[x]) {scc++;while (tmp!=x) {tmp=s[top--];belong[tmp]=scc;sz[scc]++;}}} inline void rebuild () {cnt=0; F (i,1,n) for (int j=head[i];j;j=e[j].next{int x=belong[i],y=belong[e[j].to];if (x!=y) {d[++cnt]= (edge) {h[x],y};h[x]=cnt;}}} int main () {n=read (); M=read (); F (i,1,m) {int x=read (), Y=read (); e[i]= (edge) {head[x],y};head[x]=i;} F (I,1,n) if (!dfn[i]) DFS (i); rebuild (); F (I,1,SCC) if (!h[i]) {if (ans) {ans=0;break;} else ans=sz[i];} printf ("%d\n", ans);}
bzoj1051 "HAOI2006" popular Cattle