标签:
判断是不是只有一个强连通分量就好了。。。
------------------------------------------------------------------------------------------
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define adde(u,v) add(u,v),add(v,u) const int nmax=10005; int read(){ int x=0,f=1;char c=getchar(); while(!isdigit(c)){ if(c==‘-‘) f=-1; c=getchar(); } while(isdigit(c)){ x=x*10+c-‘0‘; c=getchar(); } return x*f; } struct edge{ int from,to; edge *next; /*edge(int _from,int _to): from(_from),to(_to){};*/ }; edge e[100005],*pt,*head[nmax]; int sccno[nmax],pre[nmax],low[nmax],scc_cnt,dfn_clock,n,m; void add(int u,int v){ pt->to=v;pt->next=head[u];head[u]=pt++; } stack<int>S; void dfs(int x){ low[x]=pre[x]=++dfn_clock; S.push(x); for(edge *ee=head[x];ee;ee=ee->next){ int to=ee->to; if(!pre[to]){ dfs(to); low[x]=min(low[x],low[to]); }else if(!sccno[to]){ low[x]=min(low[x],pre[to]); } } if(pre[x]==low[x]){ scc_cnt++; while(1){ int u=S.top(); S.pop(); sccno[u]=scc_cnt; if(u==x) break; } } return ; } void init(){ clr(pre,0); dfn_clock=scc_cnt=0; clr(head,0); clr(sccno,0); pt=e; } int main(){ while(scanf("%d%d",&n,&m)==2&&n+m){ init(); rep(i,m){ int s=read(),t=read(); add(s,t); } rep(i,n){ if(!pre[i]) dfs(i); } if(scc_cnt==1) printf("Yes\n"); else printf("No\n"); } return 0; }
------------------------------------------------------------------------------------------
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
Sample Output
Source
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5183607.html