Cod sursa(job #752833)

Utilizator adalLica Adela adal Data 29 mai 2012 18:14:07
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <cstdio>
#include <vector>
#include <list>
#include <cstring>
#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;
char s[100000];
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); }
     else {
        i = G[x].back();
        Q.push_front(i);
        G[x].pop_back();
        for(it= G[i].begin(); it!=G[i].end(); ++it)
            if(*it==x) { G[i].erase(it); break;}

     }
 }

}

int main(){

    freopen("ciclueuler.in","r", stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d %d\n", &N, &M);
    for (int I=1; I<=M; ++I){
        gets(s); int n = strlen(s); ok=true;
        x=0; y=0;
        for(i=0; i<n ; ++i)
             if (s[i]==' ') ok=false;
             else {
                 if (ok) x=x*10 + s[i]-'0'; else y = y*10+s[i]-'0';
             }

        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;
}