#include<iostream> #include<algorithm> #include<vector> #include<stack> #define N 1001 using namespace std; int n,m,x,y;//顶点,边, vector<int> G[N];//动态数组 stack<int> q; int cnt[N],tpc; bool pd(){ for(int i=1;i<=n;i++){ if(cnt[i]==0){ q.push(i);//入栈 } while(!q.empty()){ int u=q.top();//栈顶 q.pop();//出顶 tpc++;//统计出栈个数 cout<<u<<" "; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; cnt[v]--; if(!cnt[v]){ q.push(v); } } if(tpc!=n)return 1; else return 0; } } } int main(){ cin>>n>>m; while(m--){ cin>>x>>y; G[x].push_back(y); cnt[y]++; } if(pd())cout<<"存在有向环"; else cout<<"不存在有向环"; return 0; }
标签:cnt,排序,int,pd,include,拓补 From: https://www.cnblogs.com/AndyYuan/p/17609556.html