您的位置:首页 > 其它

NYOJ 42 一笔画问题

2016-10-31 18:48 351 查看
http://acm.nyist.net/JudgeOnline/problem.php?pid=42

判断一个无向图是否存在欧拉通路。

首先用并查集排除图不联通的情况

如果存在欧拉回路的话,那么所有顶点的度数应该都是偶数。

如果存在欧拉通路的话,那么有且仅有两个顶点的度数是奇数,其他的都是偶数。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1000 + 20;
int Degree[maxn];
int fa[maxn];
int find(int u) {
if (fa[u] == u) return fa[u];
else return fa[u] = find(fa[u]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (x != y) fa[y] = x;
}
void work() {
memset(Degree, 0, sizeof Degree);
for (int i = 0; i <= maxn - 20; ++i) fa[i] = i;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int a, b;
scanf("%d%d", &a, &b);
Degree[a]++;
Degree[b]++;
merge(a, b);
}
int flag = 0;
for (int i = 1; i <= n; ++i) {
flag += find(i) == i;
}
if (flag != 1) {
printf("No\n");
return;
}
int odd = 0;
for (int i = 1; i <= n; ++i) {
odd += Degree[i] & 1;
}
if (odd == 0 || odd == 2) {
cout << "Yes" << endl;
} else cout << "No" << endl;
}

int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: