Problem 1001 用有向图求环不可以吗为什么ws啊?

ssauterne | 2015-05-22 23:08:20Author
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<malloc.h> int i,j,k,l,m,n,x,y,swi; int a[150][150],vis[150]; int dfs(int v){ int i=1; vis[v] = -1; for(i = 1; i <= n; i++) { if(a[v][i] != 0 && !vis[i]) { dfs(i); vis[i]=1; } if(a[v][i] != 0 && vis[i] == -1){ printf("NO\n"); swi=1; return 0; } } return 0; } int main () { while (scanf("%d %d",&n,&m)!=EOF) { memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); swi=0; for (i=1;i<=m;i++) { scanf("%d %d",&x,&y); if (x!=y) a[x][y]=1; } dfs(1); if(!swi) printf("YES\n"); } return 0; }
jijiqw | 2015-05-22 23:58:21# 1
借用哥们的代码过了
主要是这 for (i=1;i<=m;i++) { scanf("%d %d",&x,&y); if (x!=y) a[x][y]=1; if(x==y){//*************加个x==y的判断 swi=1; //break; } } if(swi) { printf("NO\n"); continue; }