标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8211 Accepted Submission(s): 3669
#include <stdio.h> #include <string.h> #include <vector> #include <stack> #define MIN(a,b) ((a)<(b)?(a):(b)) #define N 10005 using namespace std; stack<int>sta; vector<int>gra[N]; int dfn[N],low[N],now,vis[N],sum; int n,m; void ini(void) { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); now=sum=0; for(int i=1;i<=n;i++) gra[i].clear(); while(sta.empty()==0) sta.pop(); } void tarjan(int s) { vis[s]=2; dfn[s]=low[s]=++now; sta.push(s); for(int i=0;i<gra[s].size();i++) { int t=gra[s][i]; if(!dfn[t]) tarjan(t),low[s]=MIN(low[s],low[t]); else if(vis[t]==2) low[s]=MIN(low[s],dfn[t]); } if(low[s]==dfn[s]) { sum++; while(!sta.empty()) { int t=sta.top(); sta.pop(); vis[t]=1; if(t==s)break; } } } void re(void) { for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); gra[a].push_back(b); } } void run(void) { for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); if(sum>1) puts("No"); else puts("Yes"); } int main() { while(scanf("%d%d",&n,&m),n+m) { ini(); re(); run(); } }
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/4225019.html