Cod sursa(job #1713173)

Utilizator geo_furduifurdui geo geo_furdui Data 4 iunie 2016 21:42:10
Problema Ciclu Eulerian Scor 50
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 n,m,t[2][500010],g[100010],st[500010],vf=1,start[500010],k;
void citire ()
{ int x,y;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y; g[x]++; g[y]++;
        t[0][++k]=y; t[1][k]=start[x]; start[x]=k;
        t[0][++k]=x; t[1][k]=start[y]; start[y]=k;
    }
}
bool verif ()
{
    for(int i=1;i<=m;i++)
        if(g[i]%2!=0) return true;
        return false;
}
void sterge_muchia (int p,int nod)
{ int nod1=t[0][p];
    t[0][p]=-1;
    int p1=start[nod1];
    while(p1)
    {
        if(t[0][p1]==nod) {t[0][p1]=-1; break;} p1=t[1][p1];
    }
}
void dfs ()
{ st[vf]=1;
    while(vf>0)
    { int ok=0;
        int p=start[st[vf]];
        while(p)
        {
            if(t[0][p]!=-1)
            { start[st[vf]]=p;
                st[++vf]=t[0][p]; sterge_muchia(p,st[vf-1]);ok=1; break;
            } p=t[1][p];
        }
        if(ok==0) cout<<st[vf--]<<" ";
    }
}
void ce_faci ()
{
    if(verif()) cout<<1;
    else dfs();
}
int main()
{
    citire();
    ce_faci();
    return 0;
}