Cod sursa(job #2538484)

Utilizator ioana_marinescuMarinescu Ioana ioana_marinescu Data 4 februarie 2020 19:42:17
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

const int MAX_N = 100000;
const int MAX_M = 500000;

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m;
int st[MAX_M + 5];

list<int>G[MAX_N + 5];

int main() {
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        fin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    for (int i = 1; i <= n; i++)
        if (G[i].size() % 2 == 1) {
            fout << -1 << '\n';
            return 0;
        }

    int top = 1; st[top] = 1;
    while (top > 0) {
        int u = st[top];
        while (!G[u].empty()) {
            int v = G[u].front();
            G[u].pop_front();
            G[v].erase(find(G[v].begin(), G[v].end(), u));
            top++;
            st[top] = v;
            u = v;
        }
        fout << st[top] << ' ';
        top--;
    }

    return 0;
}