Pagini recente » Cod sursa (job #836401) | Cod sursa (job #60971) | Cod sursa (job #2320048) | Cod sursa (job #184794) | Cod sursa (job #1713171)
#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;
}