poj2186 Popular Cows 题解——S.B.S.
Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29642 | Accepted: 11996 |
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.
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.
* 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.
Source
——————————我是分割线————————————————————————————————
图的强联通分量。
题目大意:
有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。
给你M个关系(谁认为谁牛),求大家都认为它很牛的牛有几只。
p.s.如果牛A认为牛B很牛,牛B认为牛C很牛。那么我们就认为牛A认为牛C很牛。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 Problem: poj 2186 3 OJ: POJ 4 User: S.B.S. 5 Time: 297 ms 6 Memory: 1468 kb 7 Length: 2420 b 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<functional> 20 #include<bitset> 21 #include<vector> 22 #include<list> 23 #include<map> 24 #define maxn 10001 25 #define F(i,j,k) for(int i=j;i<=k;i++) 26 #define M(a,b) memset(a,b,sizeof(a)) 27 #define FF(i,j,k) for(int i=j;i>=k;i--) 28 #define inf 0x3f3f3f3f 29 #define maxm 50001 30 #define mod 998244353 31 //#define LOCAL 32 using namespace std; 33 int read(){ 34 int x=0,f=1;char ch=getchar(); 35 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 36 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 37 return x*f; 38 } 39 int n,m; 40 struct EDGE 41 { 42 int from; 43 int to; 44 int next; 45 }edge[maxm]; 46 int head[maxn]; 47 int dfn[maxn],low[maxn],stack1[maxn]; 48 int num[maxn],du[maxn],vis[maxn]; 49 int tim,tp,dcnt,sum; 50 int cnt,time=1,top,cut,tot; 51 inline void addedge(int u,int v) 52 { 53 edge[tot].from=u; 54 edge[tot].to=v; 55 edge[tot].next=head[u]; 56 head[u]=tot; 57 tot++; 58 } 59 inline void dfs(int u,int fa) 60 { 61 dfn[u]=time; 62 low[u]=time; 63 time++; 64 vis[u]=1; 65 stack1[top]=u; 66 top++; 67 for(int i=head[u]; i!=-1; i=edge[i].next) 68 { 69 int v=edge[i].to; 70 if(!vis[v]){ 71 dfs(v,u); 72 low[u]=min(low[u],low[v]); 73 } 74 else if(vis[v]){ 75 low[u]=min(low[u],dfn[v]); 76 } 77 } 78 if(low[u]==dfn[u]){ 79 cut++; 80 while(top>0&&stack1[top]!=u) 81 { 82 top--; 83 vis[stack1[top]]=2; 84 num[stack1[top]]=cut; 85 } 86 } 87 } 88 int main() 89 { 90 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 91 #ifdef LOCAL 92 freopen("data.in","r",stdin); 93 freopen("data.out","w",stdout); 94 #endif 95 cin>>n>>m; 96 M(head,-1); 97 F(i,0,m-1){ 98 int a,b; 99 cin>>a>>b; 100 addedge(a,b); 101 } 102 F(i,1,n) if(!vis[i]) dfs(i,0); 103 F(i,1,n)for(int j=head[i];j!=-1;j=edge[j].next){ 104 if(num[i]!=num[edge[j].to]){ 105 du[num[i]]++; 106 } 107 } 108 int x;sum=0; 109 F(i,1,cut) 110 if(!du[i]){ 111 sum++; 112 x=i; 113 } 114 if(sum==1){ 115 sum=0; 116 F(i,1,n) if(num[i]==x) sum++; 117 cout<<sum<<endl; 118 } 119 else cout<<"0"<<endl; 120 return 0; 121 }