-
L - Legal or Not
- HDU - 3342
-
#include<bits/stdc++.h> using namespace std; #define maxn 150 vector<int>mmp[maxn]; int n,m,in[maxn],u,v,ans; void topo() { queue<int>q; for(int i=0; i<n; i++) if(in[i]==0) q.push(i); while(!q.empty()) { int top=q.front(); q.pop(); for(int i=0; i<mmp[top].size(); i++) { in[mmp[top][i]]--; if(in[mmp[top][i]]==0) q.push(mmp[top][i]); } ans++; } } int main() { while(cin>>n>>m) { if(n==0) break; ans=0; memset(in,0,sizeof(in)); for(int i=0; i<n; i++) mmp[i].clear(); while(m--) { cin>>u>>v; mmp[u].push_back(v); in[v]++; } topo(); if(ans==n) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
L - Legal or Not -topo-判断存在关系环
最新推荐文章于 2020-05-21 18:50:43 发布