Cod sursa(job #1897306)

Utilizator geo_furduifurdui geo geo_furdui Data 1 martie 2017 12:24:07
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>

using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
struct bla
{
      int nod,urm;
} t[1000100];
int n,m;
int start[101000];
int st[100010],vf;
void read ()
{ int a,b,k=0;
      cin>>n>>m;
      for(int i=1;i<=m;i++)
      {
            cin>>a>>b;
            t[++k].nod=b; t[k].urm=start[a]; start[a]=k;
            t[++k].nod=a; t[k].urm=start[b]; start[b]=k;
      }
}
void up_date(int a,int b,int x)
{
      t[x].nod=-1;
      for(int y=start[a];y;y=t[y].urm)
            if(t[y].nod==b) { t[y].nod=-1; return; }
}
void df_iterativ ()
{
      vf=1; st[1]=1;
      while(vf)
      { int nr=0;
            for(int x=start[st[vf]];x;x=t[x].urm)
            {
                  if(t[x].nod!=-1)
                  {
                        st[++vf]=t[x].nod;
                        up_date(st[vf],st[vf-1],x);
                        nr=1;
                        start[st[vf-1]]=x;
                        break;
                  }
            }
            if(nr==0) {if(vf>1) cout<<st[vf]<<" "; --vf;   }
      }
}
int main()
{
    read();
    df_iterativ();
    cin.close();
    cout.close();
    return 0;
}