Cod sursa(job #2817559)

Utilizator schizofrenieShallan Davar schizofrenie Data 13 decembrie 2021 20:55:03
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

constexpr int MAXN = 1e5;
constexpr int MAXM = 5e5;

std::array<std::vector<std::pair<int, int>>, MAXN> edges;
std::array<bool, MAXM> invalid;
std::array<bool, MAXN> parity_of_degree;

std::forward_list<int> ret;

void
drop_all_invalid (std::vector<std::pair<int, int>> &v) {
    while (!v.empty() && invalid[v.back().second])
        v.pop_back();
}

int main () {
    int n, m;

    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for (int i = 0; i != m; ++ i) {
        int de, la;
        scanf("%d%d", &de, &la);
        -- de, -- la;

        edges[de].emplace_back(la, i);
        edges[la].emplace_back(de, i);
        parity_of_degree[de] ^= 1;
        parity_of_degree[la] ^= 1;
    }

    for (int i = 0; i != n; ++ i)
        if (parity_of_degree[i]) {
            puts("-1");
            return 0;
        }


    ret.emplace_front(0);
    auto it = ret.begin();

    while (it != ret.end()) {
        int start = *it;

        drop_all_invalid(edges[start]);

        if (edges[start].empty()) {
            ++ it;
            continue;
        }

        int now = start;
        do {
            int tmp = now;

            ret.emplace_after(it, tmp);

            invalid[edges[tmp].back().second] = true;
            now = edges[tmp].back().first;

            edges[tmp].pop_back();

            drop_all_invalid(edges[now]);
        } while (now != start);
    }

    ret.pop_front();
    for (int val: ret)
        printf("%d ", val + 1);
}