poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27698   Accepted: 11148

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. 
题意:有n头牛,已知m个关系即牛a认为牛b是受欢迎,现在问你在n头牛里有多少头牛被 除它外所有的牛认为是受欢迎的。
题解:
一:出度为0的SCC有0个,显然易见,该图就是一个强连通图,所有牛都满足条件;
二:出度为0的SCC有1个,满足条件的只有这一个SCC里面的牛;
三:出度为0的SCC超过1个,无论如何,出度超过0的SCC之间是无法连通的,所以没有牛满足。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
#define MAX 50010
#define INF 0x3f3f3f
using namespace std;
struct node
{
    int beg,end,next;
}edge[MAX];
int low[MAX],dfn[MAX];
int n,m,ans;
int sccno[MAX],instack[MAX];
int dfsclock,scccnt;
vector<int>newmap[MAX];
vector<int>scc[MAX];
int head[MAX];
int out[MAX];
stack<int>s;
int sum,sumout,ant;
void init()
{
    ans=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[ans].beg=u;
    edge[ans].end=v;
    edge[ans].next=head[u];
    head[u]=ans++;
}
void getmap()
{
    int a,b,i;
    while(m--)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
}
void tarjan(int u)
{
    int v,i,j;
    s.push(u);
    instack[u]=1;
    dfn[u]=low[u]=++dfsclock;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].end;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scccnt++;
        while(1)
        {
            v=s.top();
            s.pop();
            instack[v]=0;
            sccno[v]=scccnt;
            if(v==u)
            break;
        }
    }
}
void find(int l,int r)
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(instack,0,sizeof(instack));
    memset(sccno,0,sizeof(sccno));
    dfsclock=scccnt=0;
    for(int i=l;i<=r;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
}
void suodian()
{
    int i;
    ant=0;
    for(i=1;i<=scccnt;i++)
    {
        newmap[i].clear();
        out[i]=0;
    }
    for(i=0;i<ans;i++)
    {
        int u=sccno[edge[i].beg];
        int v=sccno[edge[i].end];
        if(u!=v)
        {
            newmap[u].push_back(v);
            out[u]++;
        }
    }
}
void solve()
{
    int i,j;
    sumout=0;sum=0;
    for(i=1;i<=scccnt;i++)
    {
        if(!out[i])
        {
            sumout++;
            ant=i;
        }
    }
    if(sumout==0)
    {
        printf("%d\n",n);
        return ;
    }
    else if(sumout==1)
    {
        for(i=1;i<=n;i++)
            if(sccno[i]==ant)
                sum++;
        printf("%d\n",sum);
    }
    else
    printf("0\n");
}
int main()
{
    int t;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        getmap();
        find(1,n);
        suodian();
        solve();
    }
    return 0;
}

  

posted @   非我非非我  阅读(229)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示