#include <iostream> #include <memory.h>using namespace Std;int tp[11][11],visit[11];int main () {int n,m,i,j , K,s,o,c;int Flag,count,a,b;while (cin>>n>>m) {S=1;o=0;count=0;memset (tp,0,sizeof (TP)); Memset (visit,0 , sizeof (visit)); for (i=1;i<=m;i++) {cin>>a>>b;tp[a][b]=1;} /*for (i=1;i<=n;i++) {for (j=1;j<=n;j++) if (j==n) cout<<tp[i][j]<<endl;elsecout<<tp[i][j] << "";} */while (s) {flag=0;c=0;o++;for (j=1;j<=n;j++) {for (i=1;i<=n;i++) {if (tp[i][j]==0 && visit[j]==0) { C + +;} Else{break;} }if (c==n) {flag=0;c=0;} else{flag=1;c=0;} if (flag==0) {for (k=1;k<=n;k++) tp[j][k]=0;count++;visit[j]=1;}} if (o>n) S=0;elses=1;} /*for (i=1;i<=n;i++) {for (j=1;j<=n;j++) if (j==n) cout<<tp[i][j]<<endl;elsecout<<tp[i][j] << "";} */if (count==n) cout<< "YES" <<endl;else cout<< "NO" <<ENDL;} return 0;} /* #include <iostream> #include <memory.h>using namespace Std;int tp[11][11],visit[11];int n,m,u,v;int dfs (int u) {visit[u]=-1;for (v=1;v<=n;v++) {if (tp[u][v]==1) {if (visit[v]<0) return 0;else if (! VISIT[V] &&!dfs (v)) return 0;}} Visit[u]=1;return 1;} int Topu () {for (u=1;u<=n;u++) {if (!visit[u]) {if (!dfs (u)) return 0;}} return 1;} int main () {int I;while (cin>>n>>m) {memset (tp,0,sizeof (TP)); memset (visit,0,sizeof (visit)); for (i=0;i<m;i++) {cin>>u>>v;tp[u][v]=1;} int K=topu (); if (k==0) cout<< "NO" <<endl;elsecout<< "YES" <<ENDL;} return 0;} */
Sdut OJ 2140 Graph structure Exercise--determining whether a given graph has a valid topological sequence