## $HDU$ $1878$ 无向图判定欧拉回路[模板题] ![](https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/%7Byear%7D/%7Bmonth%7D/%7Bmd5%7D.%7BextName%7D/20230808150148.png) 定理:无向图 $G$ 具有一条欧拉回路,当且仅当 $G$ 是连通的,并且所有结点度数为偶数。 思路:不需要建图。并查集统计无向图中连通块的个数,开一个数组统计每个点的度数。 ### $Code$ ```cpp {.line-numbers} // https://blog.51cto.com/u_3044148/5225981 /* 输入样例 3 3 1 2 1 3 2 3 3 2 1 2 2 3 0 输出样例 1 0 */ #include using namespace std; const int N = 1010; int n, m; int p[N]; int d[N]; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } int main() { #ifndef ONLINE_JUDGE freopen("HDU1878.in", "r", stdin); #endif while (~scanf("%d%d", &n, &m) && n) { memset(d, 0, sizeof d); for (int i = 1; i <= n; i++) p[i] = i; while (m--) { int a, b; scanf("%d%d", &a, &b); d[a]++, d[b]++; p[find(a)] = find(b); } int cnt = 0; for (int i = 1; i <= n; i++) if (p[i] == i) cnt++; int odd = 0; for (int i = 1; i <= n; i++) if (d[i] & 1) odd++; if (cnt == 1 && odd == 0) puts("1"); // 连通并且没有奇度点 else puts("0"); } return 0; } ```