Cod sursa(job #3195964)

Utilizator Radu_MocanasuMocanasu Radu Radu_Mocanasu Data 22 ianuarie 2024 12:48:26
Problema Ciclu Eulerian Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
multiset <int> G[100002];
vector <int> c;
void euler(int nod){
    while(!G[nod].empty()){
        auto it = G[nod].end();
        --it;
        int new_nod = *it;
        G[nod].erase(it);
        G[new_nod].erase(G[new_nod].find(nod));
        euler(new_nod);
    }
    c.push_back(nod);
}
int main()
{
    int n,m,i,u,v;
    fin >> n >> m;
    for(i = 1; i <= m; i++){
        fin >> u >> v;
        G[u].insert(v);
        G[v].insert(u);
    }
    for(i = 1; i <= n; i++){
        if(G[i].size() & 1){
            fout << "-1\n";
            return 0;
        }
    }
    euler(1);
    c.pop_back();
    for(auto x : c) fout << x << " ";
    return 0;
}