Files
python/TangDou/AcWing/EulerPath/POJ2230.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 10005, M = 2 * 50005;
int n, m;
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int res[M], rl;
// 4个节点组成的无向图,这里面有10条边(满的是12条边,缺少了1和3之间的两条边)
void dfs(int u) {
// 无向图求欧拉回路,见边删边,点可以重复走,最终从1出发回到1,需要走10条边,共11个点
for (int i = h[u]; ~i; i = h[u]) {
h[u] = ne[i];
dfs(e[i]);
}
res[++rl] = u;
// 打点,可以直接输出,少用内存,还好想
// printf("%d\n", u);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("POJ2230.in", "r", stdin);
#endif
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
int a, b;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
dfs(1); // 题目要求从1号点出发
for (int i = rl; i; i--) printf("%d\n", res[i]);
return 0;
}