Cod sursa(job #1927148)

Utilizator valentin50517Vozian Valentin valentin50517 Data 14 martie 2017 22:45:32
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
#define maxm 500100
#define maxn 100100
#define pb push_back
using namespace std;

int N,M,St[maxn],st;
bool B[maxn];
list<int> V[maxn];

void euler(int x){
    St[st=1] = x;
    while(st){
        x = St[st];
        for(int y;!V[x].empty();){
            y = V[x].back();
            V[x].pop_back();
            V[y].erase(find(V[y].begin(),V[y].end(),x));
            St[++st] = y;
            x = y;
        }
        cout << St[st--] << ' ';
    }
}

int main() {
	ifstream cin("ciclueuler.in");
	ofstream cout("ciclueuler.out");
    cin >> N >> M;
    for(int x,y;M--;){
        cin >> x >> y;
        V[x].pb(y);
        V[y].pb(x);
    }
    for(int i = 1;i<=N;i++) if(V[i].size()%2) return cout << -1,0;
    euler(1);
}