POJ - 2186 Popular Cows

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 
  1 #include<map>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<cstdlib>
  6 #include<queue>
  7 #include<stack>
  8 #include<vector>
  9 using namespace std;
 10 #define read(x) scanf("%lld",&x)
 11 #define out(x) printf("%lld",&x)
 12 #define cfread(x) scanf("%I64d",&x)
 13 #define cfout(x) printf("%I64d",&x)
 14 #define mian main
 15 #define min(x,y) (x<y?x:y)
 16 #define max(x,y) (x<y?y:x)
 17 #define f(i,p,q,t) for(i=p;i<q;i+=t)
 18 #define MAXN 110000
 19 #define inf 0x3f3f3f3f
 20 #define mem(x,t) memset(x,t,sizeof(x));
 21 #define T true
 22 #define F false
 23 #define def -1*inf
 24 typedef long long ll;
 25 typedef long long LL;
 26 typedef double dd;
 27 int m,n;
 28 vector<int> maps[MAXN];
 29 int vis[MAXN];//点是否在栈中
 30 int dfn[MAXN];
 31 int low[MAXN];
 32 int ans = 0;
 33 stack<int> team;
 34 int f[MAXN];
 35 int getfa(int x){
 36     return f[x]=(x==f[x])?x:getfa(f[x]);
 37 }
 38 void dfs(int x,int deep){
 39     dfn[x] = deep;
 40     low[x] = deep;
 41     vis[x] = 1;
 42     team.push(x);
 43     for(int i=0;i<maps[x].size();i++){
 44         int point = maps[x][i];
 45         if(dfn[point]>0){
 46             if(vis[point]!=0){
 47                 low[x] = min(low[x],low[point]);
 48             }
 49         }
 50         else{
 51             dfs(point,deep+1);
 52             low[x] = min(low[x],low[point]);
 53         }
 54     }
 55     if(low[x]==dfn[x]){
 56         int gs=0;
 57         int fx = getfa(x);
 58         while(!team.empty()){
 59             int p = team.top();
 60             team.pop();
 61             vis[p]=0;
 62             gs+=1;
 63             f[p]=fx;
 64             getfa(p);
 65             if(p==x){ 
 66                 break;
 67             }
 68         }
 69         if(gs>1)
 70             ans+=1;
 71     }
 72 }
 73 int cd[MAXN];
 74 int l[MAXN],r[MAXN];
 75 int main(){
 76     while(scanf("%d%d",&n,&m)==2){
 77     memset(cd,0,sizeof(cd));
 78     ans = 0;
 79     memset(vis,0,sizeof(vis));
 80     while(!team.empty())
 81         team.pop();
 82     memset(dfn,0,sizeof(dfn));
 83     memset(low,0,sizeof(low));
 84     for(int i=1;i<=n;i++)
 85         maps[i].clear(); 
 86     for(int i=1;i<=n;i++)
 87         f[i]=i;
 88     for(int i=1;i<=m;i++){
 89         int p,q;
 90         scanf("%d%d",&p,&q);
 91         maps[p].push_back(q);
 92         l[i]=p;r[i]=q;
 93     }
 94     for(int i=1;i<=n;i++){
 95         if(dfn[i]!=0);
 96         else
 97             dfs(i,1);
 98     }
 99     for(int i=1;i<=m;i++){
100         if(getfa(l[i])==getfa(r[i]))//在同一连通分量 
101             continue;
102         cd[getfa(l[i])]+=1;
103     //    cout<<l[i]<<" "<<r[i]<<" "<<getfa(l[i])endl;
104     }
105     int gs=0,pt=0;
106     for(int i=1;i<=n;i++){
107         if(i!=getfa(i))
108             continue;
109         if(cd[i]==0){
110             gs+=1;
111             pt=i;
112             if(gs>1)
113                 break;
114         }
115     }
116     if(gs!=1)
117     printf("%d\n",0);
118     else{
119         int ans = 0;
120         for(int i=1;i<=n;i++){
121             if(getfa(i)==pt)
122                 ans+=1;
123         }
124         printf("%d\n",ans);
125     }
126     }
127     return 0;
128 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024939&siteId=291194637