Cod sursa(job #1713195)

Utilizator geo_furduifurdui geo geo_furdui Data 4 iunie 2016 22:03:27
Problema Ciclu Eulerian Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>

using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
int t[2][1000010],start[1000010],n,m,sol[500010],st[500010],vf=1,vf1,k,f[100001];
void citire ()
{
    cin>>n>>m; int x,y;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y; f[x]++; f[y]++;
        ++k; t[0][k]=x; t[1][k]=start[y]; start[y]=k;
        ++k; t[0][k]=y; t[1][k]=start[x]; start[x]=k;
    } st[1]=1;
}
void sterge (int x1,int k)
{
    int nod=t[0][k]; t[0][k]=-1;
    int p=start[nod];
    while(p)
    {
        if(t[0][p]==x1) { t[0][p]=-1; break; } p=t[1][p];
    }
}
bool rez ()
{
    for(int i=1;i<=n;i++)
        if(f[i]%2!=0) return true;
    return false;
}
void dfs ()
{
    while(vf>0)
    {
        int x=start[st[vf]],ok=0;
        while(x!=0)
        { if(t[0][x]!=-1) {
            ++vf; st[vf]=t[0][x]; ok=1; sterge(st[vf-1],x); start[st[vf-1]]=x; break;}  x=t[1][x];
        }
        if(ok==0)  {sol[++vf1]=st[vf]; vf--;}
    }
}
void scrie ()
{
    for(int i=1;i<=vf1;i++)
        cout<<sol[i]<<" ";
}
int main()
{
    citire();
    if(rez()) cout<<-1;
    else
       dfs();
    scrie();
    return 0;
}