Pagini recente » Cod sursa (job #585627) | Cod sursa (job #1728941) | Cod sursa (job #637968) | Cod sursa (job #1106185) | Cod sursa (job #1908415)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int maxn=100005;
const int maxm=500005;
vector <int> G[maxn];
bool Mviz[maxm];
int N,M,from[maxn],to[maxn];
int main()
{
int i,x,y,nod;
f>>N>>M;
for (i=0;i<M;i++)
{
f>>x>>y;
G[x].push_back(i);
G[y].push_back(i);
from[i]=x;
to[i]=y;
}
for (i=1;i<=N;i++)
if (G[i].size()%2!=0) {g<<"-1";return 0;}
vector <int> ans;
vector <int> stk;
// stk.push(1);
stk.push_back(1);
while(!stk.empty())
{
nod=stk.back();
if (G[nod].empty()==0)
{
int e=G[nod].back();
G[nod].pop_back();
if (Mviz[e]==0)
{
Mviz[e]=1;
int next=from[e];
if (next==nod) next=to[e];
stk.push_back(next);
}
}
else
{
stk.pop_back();
ans.push_back(nod);
}
}
for (i=0;i<ans.size()-1;i++)
g<<ans[i]<<' ';
}