Cod sursa(job #1793133)

Utilizator delta_wolfAndrei Stoica delta_wolf Data 30 octombrie 2016 19:50:44
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#define sz(x) ((int) (x).size())
using namespace std;


vector<int> graf[100005];
int n,m,x,y,nod,e,t;
vector<int> ans,stk;
bool fol[500005];
int from[500005],to[500005];

int main()
{
    freopen("ciclueuler.in","r",stdin);
    freopen("ciclueuler.out","w",stdout);
    scanf("%d%d",&n,&m);

    for (int i = 0; i < m;i++)
    {
        scanf("%d%d",&x,&y);
        graf[x].push_back(i);
        graf[y].push_back(i);
        from[i] = x;
        to[i] = y;
    }

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

    stk.push_back(1);
    while (!stk.empty())
    {
         nod = stk.back();
        if (!graf[nod].empty())
        {
            e = graf[nod].back();
            graf[nod].pop_back();
            if (!fol[e])
            {
                fol[e]=1;
                t = ::from[e] ^ ::to[e] ^ nod;
                stk.push_back(t);
            }
        }

        else
        {
            stk.pop_back();
            ans.push_back(nod);
        }
    }

    for (int i = 0; i < sz(ans) - 1; i++)
        printf("%d ",ans[i]);
    printf("\n");

}