Cod sursa(job #752761)

Utilizator adalLica Adela adal Data 29 mai 2012 13:22:38
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cstdio>
#include <vector>
#define pb push_back


using namespace std;

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

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

  while (!G[x].empty())
    {i = G[x].back();
     G[x].pop_back();
     for(it= G[i]. begin(); it!=G[i].end(); ++it)
        if(*it==x) {G[i].erase(it); break;}
     DF(i);
     }
  Q.pb(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);
    }

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

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