Pagini recente » Cod sursa (job #766537) | Cod sursa (job #616527) | Cod sursa (job #1183269) | Cod sursa (job #1388220) | Cod sursa (job #2991299)
#include <fstream>
#include <algorithm>
#include <vector>
#define LG(x) ((int) (x).size())
#define NMAX 100005
#define MMAX 500005
using namespace std;
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
int n, m, x, y;
int from[MMAX], to[MMAX];
vector<int> G[NMAX];
vector<int> ans;
vector<int> stk;
bool usedEdge[MMAX];
int main()
{
cin>>n>>m;
for(int i=0; i<m; i++)
{
cin>>x>>y;
G[x].push_back(i);
G[y].push_back(i);
from[i]=x;
to[i]=y;
}
for (int i=1; i<=n; i++)
{
if (LG(G[i]) & 1) {
cout<<"-1"<<'\n';
return 0;
}
}
stk.push_back(1);
while(!stk.empty())
{
int node=stk.back();
if(!G[node].empty())
{
int e=G[node].back();
G[node].pop_back();
if (!usedEdge[e])
{
usedEdge[e]=true;
int to=::from[e]^::to[e]^node;
stk.push_back(to);
}
}
else
{
stk.pop_back();
ans.push_back(node);
}
}
for(int i=0; i<LG(ans)-1; i++)
{
cout<<ans[i]<<' ';
}
cout<<'\n';
}