标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29642 | Accepted: 11996 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Source
有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。
给你M个关系(谁认为谁牛),求大家都认为它很牛的牛有几只。
p.s.如果牛A认为牛B很牛,牛B认为牛C很牛。那么我们就认为牛A认为牛C很牛。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<cstdlib> 8 #include<iomanip> 9 #include<cassert> 10 #include<climits> 11 #define MAXN 10001 12 #define MAXM 50001 13 #define inf 0x7fffffff 14 #define F(i,j,k) for(int i=j;i<=k;i++) 15 #define M(a,b) memset(a,b,sizeof(a)) 16 using namespace std; 17 inline int read(){ 18 int x=0,f=1;char ch=getchar(); 19 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 20 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 21 return x*f; 22 } 23 struct node 24 { 25 int to,next; 26 }edge[MAXM]; 27 int n,m,head[MAXN],dfn[MAXN],low[MAXN],stack1[MAXN],num[MAXN],du[MAXN],vis[MAXN],cnt,time=1,top,cut; 28 int tim,tp,dcnt,sum; 29 void addedge(int u,int v) 30 { 31 edge[cnt].to=v; 32 edge[cnt].next=head[u]; 33 head[u]=cnt; 34 cnt++; 35 } 36 void dfs(int u,int fa) 37 { 38 dfn[u]=time; 39 low[u]=time; 40 time++; 41 vis[u]=1; 42 stack1[top]=u; 43 top++; 44 for(int i=head[u]; i!=-1; i=edge[i].next) 45 { 46 int v=edge[i].to; 47 if(!vis[v]) 48 { 49 dfs(v,u); 50 low[u]=min(low[u],low[v]); 51 } 52 else if(vis[v]) 53 { 54 low[u]=min(low[u],dfn[v]); 55 } 56 } 57 if(low[u]==dfn[u]) 58 { 59 cut++; 60 while(top>0&&stack1[top]!=u) 61 { 62 top--; 63 vis[stack1[top]]=2; 64 num[stack1[top]]=cut; 65 } 66 } 67 } 68 int main() 69 { 70 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 71 // freopen("data.in","r",stdin); 72 // freopen("data.out","w",stdout); 73 // int i,u,v; 74 cin>>n>>m; 75 M(head,-1); 76 F(i,0,m-1) 77 { 78 int a,b; 79 cin>>a>>b; 80 addedge(a,b); 81 } 82 F(i,1,n) 83 { 84 if(!vis[i]) dfs(i,0); 85 } 86 F(i,1,n)for(int j=head[i];j!=-1;j=edge[j].next) 87 { 88 if(num[i]!=num[edge[j].to]) 89 { 90 du[num[i]]++; 91 } 92 } 93 int x;sum=0; 94 F(i,1,cut) 95 { 96 if(!du[i]) 97 { 98 sum++; 99 x=i; 100 } 101 } 102 if(sum==1) 103 { 104 sum=0; 105 F(i,1,n) 106 { 107 if(num[i]==x) 108 { 109 sum++; 110 } 111 } 112 cout<<sum<<endl; 113 } 114 else cout<<"0"<<endl; 115 return 0; 116 }
poj2186 Popular Cows 题解——S.B.S.
标签:
原文地址:http://www.cnblogs.com/SBSOI/p/5577218.html