Pagini recente » Cod sursa (job #705999) | Cod sursa (job #2797683) | Cod sursa (job #913715) | Cod sursa (job #775107) | Cod sursa (job #1669480)
# include <cstdio>
# include <vector>
# include <stack>
using namespace std;
FILE *f = freopen("ciclueuler.in", "r", stdin);
FILE *g = freopen("ciclueuler.out", "w", stdout);
const int N_MAX = 500001;
struct muchie{
int index;
int capat;
muchie (int a, int b)
{
this->index = a;
this->capat = b;
}
};
vector <muchie> G[N_MAX];
vector <int> sol;
stack <int> stiva;
int grad[N_MAX], n, M, m;
bool viz[N_MAX];
bool OK = true;
void add_edge(int x, int y)
{
grad[x] ++;
grad[y] ++;
M++;
G[x].push_back(muchie(M, y));
G[y].push_back(muchie(M, x));
}
int delete_neighbour(int nod)
{
while (!G[nod].empty() && viz[G[nod].back().index])
G[nod].pop_back();
if (!G[nod].empty())
{
muchie muc = G[nod].back();
viz[muc.index] = true;
grad[nod] --;
grad[muc.capat] --;
G[nod].pop_back();
M--;
return muc.capat;
}
return 0;
}
void solve()
{
int start;
for (start = 1; grad[start] == 0; start ++);
stiva.push(start);
while (!stiva.empty())
{
int nod = stiva.top();
if (grad[nod])
stiva.push(delete_neighbour(nod));
else
{
sol.push_back(nod);
stiva.pop();
}
}
}
void read()
{
scanf("%d %d", &n, &m);
for (int i=1; i<=m; i++)
{
int x, y;
scanf("%d %d", &x, &y);
add_edge(x, y);
}
for (int i=1; i<=n; i++)
{
if (grad[i] % 2 == 1)
{
OK = false;
return ;
}
}
}
int main()
{
read();
if (OK)
solve();
if (M != 0)
OK = false;
if (!OK)
printf("-1\n");
else
{
//printf("%d\n", sol.size());
for (int i=0; i<sol.size() - 1; i++)
printf("%d ", sol[i]);
}
return 0;
}