Cod sursa(job #1457530)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 3 iulie 2015 16:21:15
Problema Ciclu Eulerian Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
/*
    If you can't explain it simply, you don't understand it well enough.
*/
#include <cstdio>
#include <vector>

using namespace std;

const int Nmax = 100010;

int n , m , i , x , y;
bool ok;

vector < int > g[Nmax];
vector < int > :: iterator it;

void euler(int node)
{
    int link = g[node].back();
    while (g[node].size())
    {
        g[node].pop_back();
        for (it = g[link].begin(); it != g[link].end(); ++it)
            if (*it == node)
            {
                g[link].erase(it);
                break;
            }

        euler(link);

        if (g[node].size())
            link = g[node].back();
    }
    ok ^= (node == 1);
    if (ok || node != 1) printf("%d ", node);
}

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

    for (i = 1; i <= n; ++i)
        if ((int)g[i].size()&1)
        {
            printf("-1\n");
            return 0;
        }

    euler(1);

    return 0;
}