Cod sursa(job #2961800)

Utilizator radubuzas08Buzas Radu radubuzas08 Data 7 ianuarie 2023 01:18:53
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

#define NMAX    100001
#define MMAX    500001

vector<int> graph[NMAX];

bool usedEdge[MMAX];
int from[MMAX], to[MMAX];

int main() {
    ifstream in("ciclueuler.in");
    ofstream out("ciclueuler.out");

    int n, m;
    in >> n;
    int x, y, i = 0;
    while (in >> x >> y) {
        graph[x].push_back(i);
        graph[y].push_back(i);
        from[i] = x;
        to[i] = y;
        ++i;
    }
    in.close();

    for (i = 1; i <= n; ++i) {
        if (graph[i].size() % 2) {
            out << "-1\n";
            out.close();
            return 0;
        }
    }

    vector<int> cycle;
    vector<int> s;
    s.push_back(1);
    while (!s.empty()) {
        int node = s.back();
        if (!graph[node].empty()) {
            int e = graph[node].back();
            graph[node].pop_back();
            if (!usedEdge[e]) {
                usedEdge[e] = true;
                int to = ::from[e] ^ ::to[e] ^ node;
                s.push_back(to);
            }
        } else {
            s.pop_back();
            cycle.push_back(node);
        }
    }


    for (i = 0; i < cycle.size() - 1; ++i) {
        out << cycle[i] << ' ';
    }
    out << '\n';

    out.close();
}