Pagini recente » Cod sursa (job #440520) | Cod sursa (job #339787) | Cod sursa (job #2934287) | Cod sursa (job #3184738) | Cod sursa (job #1670010)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100009;
const int mmax = 500009;
vector < int > g[nmax];
int st[mmax] , p[mmax] , mark[nmax];
int n , m , top , i , x , y , S;
int df(int x)
{
int r = 1;
for (int i = 0 ; i < g[x].size() ; ++i)
{
int y = g[x][i];
if (mark[y]) continue;
mark[y] = 1;
r += df(y);
}
return r;
}
bool check()
{
for (i = 1 ; i <= n ; ++i)
if (g[i].size() % 2)
return 0;
if (df(1) < n) return 0;
return 1;
}
void run(int x)
{
while (g[x].size())
{
int to = g[x].back();
g[x].pop_back();
for (int i = 0 ; i < g[to].size() ; ++i)
if (g[to][i] == x)
{
g[to].erase(g[to].begin() + i);
break;
}
x = to;
st[++top] = x;
}
}
void euler(int x)
{
st[++top] = x;
while (top)
{
x = st[top];
if (g[x].size()) run(x);
else
{
p[++S] = x;
top--;
}
}
for (i = 1 ; i <= S - 1 ; ++i)
printf("%d " , p[i]);
}
int main()
{
freopen("ciclueuler.in" , "r" , stdin);
freopen("ciclueuler.out" , "w" , stdout);
scanf("%d" , &n);
scanf("%d" , &m);
for (i = 1 ; i <= m ; ++i)
{
scanf("%d" , &x);
scanf("%d" , &y);
g[x].push_back(y);
g[y].push_back(x);
}
if (check())
euler(1);
else
printf("-1\n");
return 0;
}