Cod sursa(job #2029275)

Utilizator FredyLup Lucia Fredy Data 29 septembrie 2017 19:29:23
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<cstdio>
#include<vector>

using namespace std;

int k, n, m, st[500009], x, y;
vector <int> G[100009];
vector <int> ans;

void Sterg (int X, int Y)
{
    for (vector < int > :: iterator it = G[X].begin(); it != G[X].end(); it ++)
        if (*it == Y)
        {
            G[X].erase (it);
            return ;
        }
}

int main()
{

    freopen ("ciclueuler.in", "r", stdin);
    freopen ("ciclueuler.out", "w", stdout);

    scanf ("%d %d", &n, &m);
    for (int i=1; i<=m; i++)
    {
        scanf ("%d %d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for (int i=1; i<=n; i++)
        if (G[i].size()%2 == 1)
        {
            printf ("-1");
            return 0;
        }

    k=1;
    st[1]=1;
    while (k)
    {
        if (G[st[k]].empty())
            ans.push_back(st[k--]);
        else
        {
            st[++k] = G[st[k-1]][0];
            G[st[k-1]].erase(G[st[k-1]].begin());
            Sterg (st[k], st[k-1]);
        }
    }

    for (int i=0; i<ans.size() - 1; i++)
        printf ("%d ", ans[i]);
    return 0;
}