Cod sursa(job #2233719)

Utilizator AlexandruRudiAlexandru Rudi AlexandruRudi Data 24 august 2018 11:19:59
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define pb push_back
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005
#define x1 xdddddddddddddddddd
#define y1 ydddddddddddddddddd

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

int n,m,x,y,v[500005],vz[100005];

vector <pi> g[100005];

vector <int> ans[100005];

vector <int> q;

void Fill(int nod){
    if(vz[nod]) return;
    vz[nod]=1;
    for(pi i : g[nod]) Fill(i.x);
}

void DFS(int nod){
    q.push_back(nod);
    while(g[nod].sz && v[g[nod].back().y]) g[nod].pop_back();
    if(g[nod].sz){
        v[g[nod].back().y]=1;
        int nxt=g[nod].back().x;
        g[nod].pop_back();
        DFS(nxt);
    }
}

void Print(int s){
    int t;
    while(ans[s].sz){
        t=ans[s].back();
        ans[s].pop_back();
        s=t;
        out << s << ' ';
    }
}

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    in >> n >> m;
    for(int i=1;i<=m;i++){
        in >> x >> y;
        g[x].pb({y,i});
        g[y].pb({x,i});
    }
    Fill(1);
    for(int i=1;i<=n;i++){
        if(!vz[i]){
            out << -1;
            return 0;
        }
    }
    for(int i=1;i<=n;i++){
        if(g[i].sz%2){
            out << -1;
            return 0;
        }
    }
    for(int i=1;i<=n;i++){
        while(g[i].sz){
            q.clear();
            DFS(i);
            for(int i=q.sz-1;i>0;i--){
                ans[q[i-1]].pb(q[i]);
            }
        }
    }
    Print(1);
}