Cod sursa(job #2119215)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 31 ianuarie 2018 20:06:57
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 1e5 + 5;
const int M = 5e5 + 5;

int deg[N];
vector <int> edge[N];
bool viz[N];
bool used[M];
int frm[M], to[M];

vector <int> ans;

void dfs(int x){
    viz[x] = true;
    for(auto &it : edge[x]){
        if(viz[it] == false){
            dfs(it);
        }
    }
}

int main(){
//    ios_base::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 1;i <= m;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        edge[x].push_back(i);
        edge[y].push_back(i);
        frm[i] = x;
        to[i] = y;
        deg[x]++;
        deg[y]++;
    }
    //dfs(1);
    for(int i = 1;i <= n;i++){
        if(deg[i]%2 == 1){
            printf("-1");
            return 0;
        }
    }
    stack <int> s;
    s.push(1);
    while(s.empty() == false){
        int x = s.top();
        if(edge[x].empty() == false){
            int e = edge[x].back();
            edge[x].pop_back();
            if(used[e] == false){
                used[e] = true;
                int y = frm[e] ^ to[e] ^ x;
                s.push(y);
            }
        }else{
            s.pop();
            ans.push_back(x);
        }
    }
    for(auto &it : ans){
        printf("%d ", it);
    }
	return 0;
}