Cod sursa(job #2981227)

Utilizator vladm98Munteanu Vlad vladm98 Data 17 februarie 2023 15:56:48
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

bitset <500001> edges;
vector <pair <int, int>> graph[100001];
vector <int> euler;
int grad[100001];
int lastNeighbour[100001];

void dfs (int node) {
    for (int i = lastNeighbour[node]; i < graph[node].size(); ++i) {
        lastNeighbour[node] += 1;
        auto x = graph[node][i];
        if (edges[x.second]) {
            continue;
        }
        edges[x.second] = 1;
        dfs (x.first);
    }
    euler.push_back(node);
}

int main() {
    ios::sync_with_stdio(false);
    ifstream fin ("ciclueuler.in");
    ofstream fout ("ciclueuler.out");
    fin.tie(nullptr);
    fout.tie(nullptr);

    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].push_back({y, i});
        graph[y].push_back({x, i});
        grad[x] ^= 1;
        grad[y] ^= 1;
    }
    for (int i =1 ; i <= n; ++i) {
        if (grad[i]) {
            fout << -1;
            return 0;
        }
    }
    dfs (1);
    euler.pop_back();
    if (euler.size() != m) {
        cout << -1;
        return 0;
    }
    for (auto x : euler) {
        fout << x << ' ';
    }
}