题解 | #欧拉回路#
欧拉回路
https://www.nowcoder.com/practice/0ba5d8f525494a8787aaa9d53b5f9b9e
#include <iostream> using namespace std; typedef long long ll; const int N = 1010; ll p[N], d[N], h[N], g[N]; ll Find(ll k) { if (p[k] != k) p[k] = Find(p[k]); return p[k]; } void Union(ll a, ll b) { a = Find(a), b = Find(b); if (h[a] < h[b]) p[a] = b; else if (h[b] < h[a]) p[b] = a; else p[b] = a, h[a]++; } int main() { // freopen("input.txt", "r", stdin); int n, m, a, b, t; while (cin >> n && n) { cin >> m; for (int i = 0; i <= n; i++) p[i] = i, d[i] = 0; for (int i = 1; i <= m; i++) { cin >> a >> b; d[a]++, d[b]++; Union(a, b); t = a;// 记录其中一个连通图的点 } int res = 0; bool flag = true; for (int i = 1; i <= n; i++) { if (Find(i) != Find(t) && d[i] != 0) {// 存在多个连同分量 flag = false; break; } else if (d[i] & 1) {// 存在度不为偶数的点 flag = false; break; } } if (flag) puts("1"); else puts("0"); } return 0; }