Cod sursa(job #1318755)

Utilizator bodyionitaIonita Bogdan Constantin bodyionita Data 16 ianuarie 2015 12:27:53
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> G[100001], Sol;
int N, M, x, y, Stiva[500001], i;
inline int verif()
{
    for (int i=1; i<=N; i++)
        if (G[i].size()%2==1) return 0;
    return 1;
}
inline void DF()
{
    int x=1,y;
    Stiva[++Stiva[0]]=x;
    while (Stiva[0])
    {
        x=Stiva[Stiva[0]];
        if (!G[x].empty())
        {
            y=G[x].back();
            Stiva[++Stiva[0]]=y;
            G[x].pop_back();
            G[y].erase(find(G[y].begin(), G[y].end(), x) );
        } else
        {
            Stiva[0]--;
            Sol.push_back(x);
        }
    }
}
int main()
{
    freopen("ciclueuler.in", "r", stdin);
    freopen("ciclueuler.out", "w", stdout);
    scanf("%d%d", &N, &M);
    for (i=1; i<=M; i++)
    {
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    if (!verif()) printf("-1\n");
    else
    {
        DF();
        for (i=0; i<Sol.size()-1; i++)
            printf("%d ", Sol[i]);
    }
    return 0;
}