C語言源碼: [cpp] #include<stdio.h> #define maxsize 110 int E[maxsize][maxsize]; int Indegree[maxsize]; int Stack[maxsize]; int main() { int n,m,i,j,a,b,num,top; scanf("%d %d",&n,&m); while(n) { for(i=0;i<n;i++) for(j=0;j<n;j++) E[i][j]=0; while(m>0) { scanf("%d %d",&a,&b); E[a][b]=1; m--; } for(i=0;i<n;i++) Indegree[i]=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(E[i][j]==1) Indegree[j]++; } } top=0; for(i=0;i<n;i++) if(Indegree[i]==0) Stack[top++]=i; num=0; while(top>0) { i=Stack[--top]; num++; for(j=0;j<n;j++) { if(E[i][j]==1) { Indegree[j]--; if(Indegree[j]==0) Stack[top++]=j; } } } if(num==n) printf("YES\n"); else printf("NO\n"); scanf("%d %d",&n,&m); } }