Cod sursa(job #1693757)

Utilizator llalexandruLungu Alexandru Ioan llalexandru Data 23 aprilie 2016 20:18:07
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <list>
#define NM 500005

using namespace std;

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

list <int> G[NM];
list <int>::iterator it;
int S[NM];

int n, m, x, y, fi;

bool Verif()
{
    int i;
    for (i=1; i<=n; i++)
        if(G[i].size()==0 || G[i].size()%2==1)
            return 1;
    return 0;
}

void DFS(int x)
{
    int nx;
    fi++;
    S[fi]=x;
    if (G[x].size()>0)
    {
        nx=*G[x].begin();
        G[x].erase(G[x].begin());
        for (it=G[nx].begin(); it!=G[nx].end(); it++)
            if (*it==x)
            {
                G[nx].erase(it);
                break;
            }
        DFS(nx);
    }
}

void Euler()
{
    int act;
    fi=1;
    S[1]=1;
    while (fi>0)
    {
        act=S[fi];
        fi--;
        if (G[act].size()==0)
        {
            fout<<act<<" ";
        }
        else
        {
            DFS(act);
        }
    }
}

int main()
{
    int x, y, i;
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>x>>y;

        G[x].push_back(y);
        G[y].push_back(x);
    }
    if (Verif())
        fout<<-1;
    else
    {
        Euler();
    }
    return 0;
}