Pagini recente » Cod sursa (job #818646) | Cod sursa (job #893278) | Cod sursa (job #2071976) | Cod sursa (job #2797057) | Cod sursa (job #2244207)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
const int VMAX=100009,EMAX=500009;
int v,e,i,x,y,from[EMAX],to[EMAX],sol[EMAX],nod,muchie,used[EMAX],k;
vector < int > g[VMAX];
stack < int > sk;
int main()
{
fin >> v >> e;
for(i = 1; i <= e; i++)
{
fin >> x >> y;
g[x].pb(i);
g[y].pb(i);
from[i] = x;
to[i] = y;
}
for(i = 1; i <= v ; i++)
if(g[i].size() & 1)
{
fout << -1;
return 0;
}
sk.push(1);
while(!sk.empty())
{
nod=sk.top();
if(!g[nod].empty())
{
int muchie = g[nod].back();
g[nod].pop_back();
if(!used[muchie])
{
used[muchie] = 1;
if(from[muchie]==nod)
sk.push(to[muchie]);
else
sk.push(from[muchie]);
}
}
else
{
sol[++k]=nod;
sk.pop();
}
}
for(i = 1; i < k; i++)
fout << sol[i] << " ";
}