Cod sursa(job #1457553)

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

using namespace std;

const int Nmax = 100010;

int n , m , i , x , y;

vector < int > g[Nmax];
stack < int > S;

void euler(int node)
{
    while (g[node].size())
    {
        S.push(node);

        int link = g[node].back();

        g[node].pop_back();
        for (int i = 0; i < g[link].size(); ++i)
            if (g[link][i] == node)
            {
                g[link].erase(g[link].begin()+i);
                break;
            }

        node = link;
    }
}

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

    bool first = 1;
    for (int node = 1; !S.empty() || first; first = 0, printf("%d ", node))
    {
        euler(node);

        node = S.top(); S.pop();
    }

    return 0;
}