Test instructions: Give n,m, the number of people is 0 to n-1, and then give M relations, ask can not be able to do topological sorting
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace Std;int indegree[1005],queue[1005],head[1005];struct edge{int to;int next;} Edge[1005];int main () {int N,m,i,j,k,iq,a,b;while (scanf ("%d%d", &n,&m)!=eof&&n&&m) {memset (head,-1,sizeof (head) ); memset (Indegree,0,sizeof (Indegree)); for (i=1;i<=m;i++) {scanf ("%d%d", &a,&b); Edge[i].to=b;edge[i]. next=head[a];head[a]=i;indegree[b]++;} Iq=0;for (i=0;i<n;i++) {if (indegree[i]==0) {queue[iq++]=i;}} for (i=0;i<iq;i++) {for (K=head[queue[i]];k!=-1;k=edge[k].next) {indegree[edge[k].to]--;if (indegree[edge[k].to] ==0) {queue[iq++]=edge[k].to;}}} if (iq==n) printf ("yes\n"); elseprintf ("no\n");}}
HDU 3342 Legal or not "topological sort"