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.
题解:给你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 }