## Output

 1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 #include <stack>
6 #define rep(i,l,r) for(int i=l; i<=r; i++)
7 #define clr(x,y) memset(x,y,sizeof(x))
8 #define travel(x) for(int i=last[x]; i; i=edge[i].pre)
9 using namespace std;
10 const int INF = 0x3f3f3f3f;
11 const int maxn = 10010;
12 struct Edge{
13     int pre,to;
14 }edge[50010];
15 int n,m,x,y,tot=0,ans,scc=0,dfsclock=0,last[maxn],dfn[maxn],low[maxn],belong[maxn],cnt[maxn];
16 bool isin[maxn],oud[maxn];
17 stack <int> s;
19     int ans = 0, f = 1;
20     char c = getchar();
21     while (!isdigit(c)){
22         if (c == '-') f = -1;
23         c = getchar();
24     }
25     while (isdigit(c)){
26         ans = ans * 10 + c - '0';
27         c = getchar();
28     }
29     return ans * f;
30 }
31 inline void addedge(int x,int y){
32     edge[++tot].pre = last[x];
33     edge[tot].to = y;
34     last[x] = tot;
35 }
36 void tarjan(int x){
37     dfn[x] = low[x] = ++dfsclock;
38     isin[x] = 1; s.push(x);
39     travel(x){
40         if (!dfn[edge[i].to]){
41             tarjan(edge[i].to);
42             low[x] = min(low[x],low[edge[i].to]);
43         }
44         else if (isin[edge[i].to]) low[x] = min(low[x],dfn[edge[i].to]);
45     }
46     if (low[x] == dfn[x]){
47         scc++;
48         while (s.top() != x){
49             isin[s.top()] = 0;
50             belong[s.top()] = scc;
51             s.pop();
52             cnt[scc]++;
53         }
54         isin[x] = 0; belong[x] = scc; s.pop(); cnt[scc]++;
55     }
56 }
57 int main(){
60     clr(dfn,0); clr(cnt,0);
61     rep(i,1,n) if (!dfn[i]) tarjan(i);
62     clr(oud,0);
63     rep(now,1,n) travel(now) if (belong[now] != belong[edge[i].to]) oud[belong[now]] = 1;
64     ans = 0;
65     rep(i,1,scc) if (!oud[i]){
66         if (ans){ans = 0; break;}
67         ans = cnt[i];
68     }
69     printf("%d\n",ans);
70     return 0;
71 }
View Code

posted on 2015-12-01 17:38  ACMICPC  阅读(179)  评论(0编辑  收藏