Cod sursa(job #752765)

Utilizator adalLica Adela adal Data 29 mai 2012 14:24:19
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>
#include <vector>
#include <list>
#define pb push_back


using namespace std;

vector<int> G[100010];
list <int> Q;
int nc, nr, i, k, N, M, x, y;
bool ok;

void DF(int x)
{
int i;

vector<int>::iterator it;
Q.pb(x);

while (!Q.empty()){

     x = Q.front();

     if (G[x].empty()){Q.pop_front(); printf("%d ", x); continue; }
     else {
        i = G[x].back();
        Q.push_front(i);

        for(it= G[i].begin(); it!=G[i].end(); ++it)
            if(*it==x) {G[i].erase(it); break;}
        G[x].pop_back();

     }
 }

}

int main(){

    freopen("ciclueuler.in","r", stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d %d\n", &N, &M);
    for (i=1; i<=M; ++i){
        scanf("%d %d\n", &x, &y);
        G[x].pb(y);
        G[y].pb(x);
    }

    ok=true;
    for(i=1; i<=N; i++)
        if (G[i].size()%2) ok=false;

    if (!ok) printf("-1\n");
    else DF(1);
    return 0;
}