POJ 2186 Popular cows(SCC 缩点)

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. 
 
题解:给你M组u,v代表v是受u欢迎的,并且欢迎具有传递性;让你求最多有多少人是相互受欢迎的;
SCC + 缩点;强连通分量跑一边,然后缩点,就可转化为DAG图;然后记录每个 “ 点 ” 的出度;如果有超过一个,那么输出0即可;
如果为一个,则输出其所在点集的大小;
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 #include<vector>
10 #include<set>
11 #include<map>
12 using namespace std;
13 typedef long long LL;
14 const int INF=0x3f3f3f3f;
15 #define eps 1e-8
16 typedef pair<int,int> P;
17 const int maxn=5e4+10;
18 int N,M,u[maxn],v[maxn],tot,times,blocks;
19 int head[maxn],dfn[maxn],lowv[maxn];
20 int ins[maxn],outd[maxn],belong[maxn],sz[maxn];
21 struct Node{
22     int v,nxt;
23 } edge[maxn];
24 stack<int> st;
25 void Init()
26 {
27     memset(head,-1,sizeof head);
28     memset(dfn,0,sizeof dfn);
29     memset(lowv,0,sizeof lowv);
30     memset(ins,0,sizeof ins);
31     memset(outd,0,sizeof outd);
32     memset(sz,0,sizeof sz);
33     memset(belong,0,sizeof belong);
34     tot=times=blocks=0;
35     while(!st.empty()) st.pop();
36 } 
37 
38 void Addedge(int u,int v)
39 {
40     edge[tot].v=v;
41     edge[tot].nxt=head[u];
42     head[u]=tot++;
43 }
44 
45 void Tarjan(int u)
46 {
47     dfn[u]=lowv[u]=++times;
48     st.push(u);
49     ins[u]=1;
50     for(int i=head[u];~i;i=edge[i].nxt)
51     {
52         int v=edge[i].v;
53         if(!dfn[v]) Tarjan(v),lowv[u]=min(lowv[u],lowv[v]);
54         else if(ins[v]) lowv[u]=min(lowv[u],dfn[v]);
55     }
56     
57     if(dfn[u]==lowv[u])
58     {
59         ++blocks;
60         int v;
61         do
62         {
63             v=st.top(); st.pop();
64             belong[v]=blocks;
65             sz[blocks]++;
66             ins[v]=0;
67         } while(u!=v);
68     }
69     
70 }
71 
72 int main()
73 {
74     ios::sync_with_stdio(false);
75     cin>>N>>M;
76     Init();
77     for(int i=1;i<=M;i++)
78     {
79         cin>>u[i]>>v[i];
80         Addedge(u[i],v[i]);
81     }
82     int cnt=0,flag;
83     for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
84     for(int i=1;i<=M;i++) if(belong[u[i]]!=belong[v[i]]) outd[belong[u[i]]]++;
85     for(int i=1;i<=blocks;i++) if(outd[i]==0) cnt++,flag=i;
86         
87     if(cnt!=1) cout<<0<<endl;
88     else cout<<sz[flag]<<endl;
89     return 0;
90  } 
View Code

 

转载于:https://www.cnblogs.com/songorz/p/9632898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值