POJ-2186 Popular Cows(tarjan+缩点)

Popular Cows

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 38927   Accepted: 15861

Description

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. 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long

const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int N = 10010;

int n,m,tot,ans,num,temp;
int vis[N],low[N],dfn[N],color[N],out[N],cnt[N];
vector<int>G[N];
stack<int>S;
void init(){
    ans = tot = num = temp = 0;
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(out,0,sizeof(out));
    memset(cnt,0,sizeof(cnt));
    memset(color,0,sizeof(color));
    while(!S.empty())   S.pop();
    for(int i=0;i<=n;i++)   G[i].clear();
}
void tarjan(int x){
    low[x] = dfn[x] = ++tot;
    S.push(x);
    vis[x] = 1;
    for(int i=0;i<G[x].size();i++){
        int v = G[x][i];
        if(!dfn[v]){
            tarjan(v);
            low[x] = min(low[x],low[v]);
        }
        else if(vis[v]){
            low[x] = min(low[x],low[v]);
        }
    }
    if(low[x] == dfn[x]){
        num ++;
        while(1){
            int now = S.top();
            S.pop();
            color[now] = num;
            vis[now] = 0;
            if(now == x){
                break;
            }
        }
        ans++;
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        int u,v;
        for(int i=0;i<m;i++){
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
        }
        for(int i=1;i<=n;i++){
            if(!dfn[i]) tarjan(i);
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<G[i].size();j++){
                int v = G[i][j];
                if(color[v]!=color[i]){
                    out[color[i]]++;
                }
            }
            cnt[color[i]]++;
        }
        for(int i=1;i<=num;i++){
            if(out[i]==0){
                temp++;
                ans = cnt[i];
            }
        }
        if(temp == 0)   cout<<"0"<<endl;
        else {
            if(temp>1)  cout<<"0"<<endl;
            else cout<<ans<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81123230
今日推荐