P6066 [USACO05JAN] Watchcow S

发布时间 2023-09-27 15:06:29作者: carp_oier

prologue

这个题这么水的一个板子题。

analysis

这个题目我们正反建两条边,在跑欧拉回路的时候,看这个边是不是被走过,走过就不走,跳过这个边。如果没走,就走这条边并且标记这个边走过了。

code time

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rl register ll 

const ll N = 2e4 + 10, M = 1e5 + 10;

ll n, m;

ll tot, ne[M], e[M], h[N], ans[M], cnt;

bool rm[M];

inline void add(ll a, ll b)
{
    ne[++tot] = h[a], h[a] = tot, e[tot] = b;
}

inline void dfs(ll u)
{
    for(rl i=h[u]; ~i; i = ne[i])
    {
        if(rm[i]) continue;

        rm[i] = true;

        ll v = e[i];
        dfs(v);
    }
    ans[++cnt] = u;
}

int main()
{
    freopen("1.in", "r", stdin), freopen("1.out", "w", stdout);
    cin >> n >> m;
    
    memset(h, -1, sizeof h);

    for(rl i=1; i <= m; ++ i)
    {
        ll a, b;
        cin >> a >> b;
        add(a, b), add(b, a);
    }

    dfs(1);

    for(rl i=cnt; i; -- i) cout << ans[i] << endl;
    return 0;
}