标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8487 Accepted Submission(s): 3797
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; #define maxn 10000+10 vector<int> mp[maxn]; stack<int> s; int n,m,Dfs[maxn],low[maxn]; bool isstack[maxn]; int use[maxn]; int top,newflag; void init() { memset(isstack,0,sizeof(isstack)); memset(use,0,sizeof(use)); memset(Dfs,0,sizeof(Dfs)); memset(low,0,sizeof(low)); for(int i=0;i<maxn;i++) mp[i].clear(); while(!s.empty()) s.pop(); newflag=0; top=0; } void tarjan(int u) { Dfs[u]=low[u]=++top; isstack[u]=1; s.push(u); for(int x=0;x<mp[u].size();x++) { int v=mp[u][x]; if(!Dfs[v]) { tarjan(v); if(low[v]<low[u]) low[u]=low[v]; } else if(isstack[v]&&Dfs[v]<low[u]) { low[u]=Dfs[v]; } } if(Dfs[u]==low[u]) { newflag++; int x; do { x=s.top(); isstack[x]=0; use[x]=newflag; s.pop(); }while(x!=u); } return ; } int main() { int x,y; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; init(); for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); mp[x].push_back(y); } for(int i=1;i<=n;i++) { if(!Dfs[i]) tarjan(i); } if(newflag==1) { printf("Yes\n"); } else printf("No\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4346957.html