Cod sursa(job #752682)

Utilizator adalLica Adela adal Data 29 mai 2012 10:40:50
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>
#include <deque>
#define pb push_back


using namespace std;

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

void DF(int x)
{
int i;
deque<int>::iterator it;

  while (!G[x].empty())
    {i = G[x].front();
     G[x].pop_front();
     for(it= G[i]. begin(); it!=G[i].end(); ++it)
        if(*it==x) {G[i].erase(it); break;}
     DF(i);
     }
  Q[++nr]=x;
}

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);
        gr[x]++;
    }

    nr=0; ok=true;
    for(i=1; i<=N; i++)
        if (gr[i]%2) ok=false;

    if (!ok) printf("-1\n");
    else {
        DF(1);
        for(i=1; i<=nr; ++i) printf("%d ", Q[i]);
        printf("\n");
    }
    return 0;
}