Cod sursa(job #2247770)

Utilizator pinteastefanPintea Teodor Stefan pinteastefan Data 29 septembrie 2018 01:31:10
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;
vector < pair<int, int>> graff[100001];
int vizitate[500001];
vector <int> sol;
int grad [100100];
void dfs(int nod){
//    cout << nod << '\n';
    while (graff [nod].size() and vizitate [graff[nod].back().second])
        graff[nod].pop_back();
    if (graff[nod].size())
    {
        vizitate[graff[nod].back().second] = 1;
        dfs(graff[nod].back().first);
    }
//    else {
        sol.push_back(nod);
//    }
}

int main() {
    ifstream f("ciclueuler.in");
    ofstream g("ciclueuler.out");
    int n, m;
    f >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        f >> x >> y;
        graff[x].emplace_back(y, i);
        graff[y].emplace_back(x, i);
        grad[x] += 1;
        grad[y] += 1;
    }
    for (int i = 1; i <= n; ++i)
    {
        if (grad[i] % 2 == 1) {
            g << -1;
            return 0;
        }
    }
    dfs(1);
    sol.pop_back();
    for (auto &x : sol)
        g << x << ' ';
    return 0;
}