Pagini recente » Cod sursa (job #985194) | Cod sursa (job #2821677) | Cod sursa (job #805275) | Cod sursa (job #565416) | Cod sursa (job #2032577)
#include <fstream>
#include <cstdio>
#include <stack>
#include <set>
#define VAL 100005
using namespace std;
int N, M, i, j;
int A, B, nod, nr;
int X[VAL];
stack <int> S;
multiset <int> G[VAL];
multiset <int> :: iterator it;
bool ciclu=true;
char buff[VAL];
int poz=0;
int Read_INT()
{
int nr=0;
while (buff[poz]<'0' || buff[poz]>'9')
if (++poz==VAL)
fread(buff, 1, VAL, stdin), poz=0;
while ('0'<=buff[poz] && buff[poz]<='9')
{
nr=nr*10+buff[poz]-'0';
if (++poz==VAL)
fread(buff, 1, VAL, stdin), poz=0;
}
return nr;
}
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
N=Read_INT();
M=Read_INT();
for (i=1; i<=M; i++)
{
A=Read_INT();
B=Read_INT();
X[A]++;
X[B]++;
G[A].insert(B);
G[B].insert(A);
}
for (i=1; i<=N; i++)
{
if (X[i] % 2==1)
{
ciclu=false;
break;
}
}
if (ciclu==false)
printf("-1\n");
else
{
nod=1;
while (1)
{
if (G[nod].empty()==true && S.empty()==true)
break;
if (G[nod].empty()==true)
{
printf("%d ", nod);
nod=S.top();
S.pop();
}
else
{
S.push(nod);
it=G[nod].begin();
nr=*it;
G[nod].erase(it);
G[nr].erase(G[nr].find(nod));
nod=nr;
}
}
}
return 0;
}