这道题我用了邻接矩阵,这道题会有x->x的情况,这个不算在度内,所以要有:
for (int i=1;i<=n;i++) p[i][i]=0;
坑了我3次WA...#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const int N=1000; int n,m,p[N][N]; int v[N]; int DFS(int u) { int c=0,d=1; v[u]++; for (int i=1;i<=n;i++) if (p[u][i]) { c++; if (!v[i]) { d&=DFS(i); if (!d) break; } } return d&&c%2==0; } int main(void) { for (;scanf("%d%d",&n,&m)==2;) { memset(p,0,sizeof p); memset(v,0,sizeof v); int x,y; for (;m--;) { scanf("%d%d",&x,&y); p[x][y]=p[y][x]=1; } for (int i=1;i<=n;i++) p[i][i]=0; int d=DFS(1); for (int i=1;i<=n;i++) { if (!d) break; if (!v[i]) d=0; } printf("%d\n",d); } return 0; }
原文地址:http://blog.csdn.net/u013598409/article/details/44229115