hdu_5154 拓扑
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> using namespace std; int n,m; const int maxn=110; vector<int> g[maxn]; int du[maxn]; bool topsort() { memset(du,0,sizeof(du)); for( int i=1;i<=n;i++){ for( int j=0;j<g[i].size();j++) du[g[i][j]]++; } int tot=0; queue<int>q; for(int i=1;i<=n;i++) if(!du[i]) q.push(i); // printf("%d",q.front()); while(!q.empty()){ int x=q.front(); q.pop(); tot++; for(unsigned int j=0;j<g[x].size();j++){ int t = g[x][j]; du[t]--; if(!du[t]) q.push(t); } } if(tot==n) return 1; return 0; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ int x,y; int state=1; for(int i=0;i<maxn;i++) g[i].clear(); for(int i=0;i<m;i++){ scanf("%d%d",&x,&y); g[x].push_back(y); } state = topsort(); if(state==1) printf("YES\n"); else printf("NO\n"); } return 0; }