You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

2 years ago
## [$POJ$ $2230$ $Watchcow$](http://poj.org/problem?id=2230)
### 一、大致题意:
有一个人每晚要检查牛场,牛场内有$m$条路,他担心会有遗漏,就每条路检查两次,且每次的方向不同,要求你打印他行走的路径(必须从$1$开始),打印一条即可。
![](https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/%7Byear%7D/%7Bmonth%7D/%7Bmd5%7D.%7BextName%7D/20230804165218.png)
### 二、实现代码
```cpp {.line-numbers}
#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;
}
```