Codeforces 1152E(欧拉路径)

Posted alphawa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1152E(欧拉路径)相关的知识,希望对你有一定的参考价值。

看样例然后发现只要求一个一笔画即可,用板子。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 1e5 + 5;
int n, b[maxn], c[maxn], _b[maxn], _c[maxn];
int d[maxn << 1], tot;
vector<pair<int, int>> adj[maxn << 1];
bool vis[maxn];
int ans[maxn];
int cnt;

void dfs(int cur) {
    while (!adj[cur].empty()) {
        auto tmp = adj[cur].back();
        adj[cur].pop_back();
        if (!vis[tmp.second]) {
            vis[tmp.second] = 1;
            dfs(tmp.first);
            ans[cnt++] = tmp.first;
        }
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++)
        scanf("%d", &b[i]), d[++tot] = b[i];
    for (int i = 1; i < n; i++)
        scanf("%d", &c[i]), d[++tot] = c[i];

    sort(d + 1, d + 1 + tot);
    tot = unique(d + 1, d + 1 + tot) - d - 1;
    for (int i = 1; i < n; i++) {
        _b[i] = lower_bound(d + 1, d + 1 + tot, b[i]) - d;
        _c[i] = lower_bound(d + 1, d + 1 + tot, c[i]) - d;
        if (_b[i] > _c[i]) {
            puts("-1");
            return 0;
        }
        adj[_b[i]].push_back({_c[i], i});
        adj[_c[i]].push_back({_b[i], i});
    }

    vector<int> v;
    for (int i = 1; i <= tot; i++) {
        if (adj[i].size() % 2 == 1)
            v.push_back(i);
    }
    if (v.size() == 2) {
        dfs(v[0]);
        ans[cnt++] = v[0];
    } else if(v.size() == 0) {
        dfs(1);
        ans[cnt++] = 1;
    }
    if (cnt == n)
        for (int i = cnt - 1; ~i; --i)
            printf("%d ", d[ans[i]]);
    else    puts("-1");
    return 0;
}

以上是关于Codeforces 1152E(欧拉路径)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 789D 欧拉路径计数,思维

Codeforces 1038E Maximum Matching

Codeforces Round #375(div 2)

D - Notepad CodeForces - 17D (欧拉降幂)

Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路

Underground Lab CodeForces - 782E (欧拉序)