Cod sursa(job #2444892)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 1 august 2019 17:56:10
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;

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

vector <int> a[100005], ans, used[100005];
stack <int> s;

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i) {
        if(a[i].size() % 2 || !a[i].size()) {
            fout << "-1";
            return 0;
        }
    }
    for(int i = 1; i <= n; ++i) sort(a[i].begin(), a[i].end());
    s.push(1);
    vector <int>::iterator it;
    while(!s.empty()) {
        int x = s.top();
        if(!a[x].empty()) {
            int v = *a[x].begin();
            vector <int>::iterator it = lower_bound(a[v].begin(), a[v].end(), x);
            a[x].erase(a[x].begin());
            a[v].erase(it);
            s.push(v);
        }
        else {
            s.pop();
            ans.push_back(x);
        }
    }
    ans.pop_back();
    for(auto v : ans) fout << v << " ";
    return 0;
}