UVA 10596 清晨漫步

来源:互联网 发布:中世纪2全面战争优化 编辑:程序博客网 时间:2024/04/29 10:00

简单题,求无向图是否存在欧拉回路

1.判断结点度数是否为偶数

2.dfs判断图的连通性

但是为什么另外一种写法(注释)会WA呢?

 

#include<iostream>#include<cstdio>#include<cstring>#include<memory>#include<queue>#include<algorithm>#include<string>#include<cmath>#include<stack>using namespace std;///const int maxn = 210;bool maze[maxn][maxn];int N, R;bool vis[maxn];int degrees[maxn];int cc;//dfs检查图的连通性/*bool dfs(int start, int total){if (total == N)return true;int i;vis[start] = true;for (i = 0; i < N; i++){if ((maze[start][i]) && !vis[i])  //何去何从??{if (dfs(i, total + 1))return true;}}return false;}*/void dfs (int cnt){vis[cnt] = true;cc++;for (int i = 0; i < N; i++)if (!vis[i] && maze[cnt][i])dfs (i);}bool check(){int i, pos;for (pos = 0; pos < N; pos++)if (degrees[pos] != 0)break;dfs(pos);if (cc != N)return false;for (i = 0; i < N; i++){if (degrees[i] % 2 != 0)return false;}return true;}int main(){///int i, j;int pos;while (cin >> N >> R){cc = 0;memset(maze, 0, sizeof(maze));memset(vis, 0, sizeof(vis));memset(degrees, 0, sizeof(degrees));//输入边for (i = 0; i < R; i++){int from, to;cin >> from >> to;maze[from][to] = maze[to][from] = true;  //边的表示何去何从degrees[from]++;degrees[to]++;}if (check())cout << "Possible" << endl;elsecout << "Not Possible" << endl;}///    return 0;}


 

原创粉丝点击