标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define inf 2000000001 int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } struct is { int u,v; int next; }edge1[100010],edge2[100010]; int flag1[10010],flag2[10010]; int jiedge1,jiedge2; int jidian1,jidian2; int head1[100010],head2[100010]; int shun1[100010],shun2[100010]; int belong[100010]; int n,m; void update(int x,int y) { jiedge1++; edge1[jiedge1].u=x; edge1[jiedge1].v=y; edge1[jiedge1].next=head1[x]; head1[x]=jiedge1; jiedge2++; edge2[jiedge2].u=y; edge2[jiedge2].v=x; edge2[jiedge2].next=head2[x]; head2[x]=jiedge2; } void dfs1(int x) { flag1[x]=1; for(int i=head1[x];i;i=edge1[i].next) { if(!flag1[edge1[i].u]) dfs1(edge1[i].u); } shun1[++jidian1]=x; } void dfs2(int x) { flag2[x]=1; belong[x]=jidian2; for(int i=head2[x];i;i=edge2[i].next) { if(!flag2[edge2[i].u]) dfs2(edge2[i].u); } } void Kosaraju() { memset(flag1,0,sizeof(flag1)); memset(flag2,0,sizeof(flag2)); jidian1=0; for(int i=1;i<=n;i++) if(!flag1[i]) dfs1(i); jidian2=1; for(int i=jidian1;i>0;i--) { if(!flag2[i]) { dfs2(shun1[i]); jidian2++; } } } int main() { int i; while(~scanf("%d%d",&n,&m)) { memset(head1,0,sizeof(head1)); memset(head2,0,sizeof(head2)); if(n==0&&m==0)break; jiedge1=0; jiedge2=0; for(i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); update(u,v); } Kosaraju(); /*for(i=1;i<=n;i++) cout<<belong[i]<<" "; cout<<endl;*/ int lu=0,ans=belong[1]; for(i=2;i<=n;i++) if(belong[i]!=ans) { lu=1; break; } if(lu) printf("No\n"); else printf("Yes\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5357445.html