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.

1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

POJ 2230 Watchcow

一、大致题意:

有一个人每晚要检查牛场,牛场内有m条路,他担心会有遗漏,就每条路检查两次,且每次的方向不同,要求你打印他行走的路径(必须从1开始),打印一条即可。

二、实现代码

#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;
}