Cod sursa(job #1711997)

Utilizator geo_furduifurdui geo geo_furdui Data 1 iunie 2016 19:15:19
Problema Ciclu Eulerian Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 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,n1[10000001],f[100010];
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 x,int x1,int k)
{
    if(t[0][k+1]==x1) t[0][k+1]=-1; else t[0][k-1]=-1;
}
bool rez ()
{
    for(int i=1;i<=n;i++)
        if(f[i]%2) return true;
    return false;
}
void dfs ()
{ n1[1]=start[1];
    while(vf>0)
    {
        int x=n1[vf],ok=0;
        while(x!=0)
        { if(t[0][x]!=-1) {
            ++vf; st[vf]=t[0][x]; ok=1; int a=t[0][x]; t[0][x]=-1; sterge(a,st[vf-1],x); n1[vf-1]=x; n1[vf]=start[st[vf]]; 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;
}