Cod sursa(job #2323213)

Utilizator tnocNume corect tnoc Data 18 ianuarie 2019 22:43:30
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <list>
#include <algorithm>
#define NMAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,vf,x,y,st[NMAX*5];
list<int> v[NMAX];
int main()
{
    f>>n>>m;
    for(int i=1; i<=m; ++i)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1; i<=n; ++i)
        if(v[i].size()%2)
        {
            g<<-1<<'\n';
            return 0;
        }
    vf=1,st[vf]=1;
    while(vf>0)
    {
        x=st[vf];
        if (!v[x].empty()) {
            y=v[x].front();
            v[x].pop_front();
            v[y].erase(find(v[y].begin(),v[y].end(),x));
            st[++vf]=y;
        }
        else
            g<<st[vf--]<<' ';
    }
    return 0;
}