Pagini recente » Cod sursa (job #727239) | Cod sursa (job #1171918) | Cod sursa (job #2409302) | Cod sursa (job #2440868) | Cod sursa (job #1896284)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct _muchie{
int a, b;
} muchie[500001]={0};
vector<int> v[100001],stack;
bool viz[500001];
int sol[500001], lg, w;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int main()
{
int i, n, m, nod, w, a, b;
f>>n>>m;
for(i=1; i<=m; i++)
{f>>a>>b;
v[a].push_back(i);
v[b].push_back(i);
muchie[i].a = a;
muchie[i].b = b;
}
for(i=1; i<=n; i++)
if(v[i].size() % 2){
g<<"-1";
return 0;
}
stack.push_back(1);
while(!stack.empty())
{nod = stack.back();
if(!v[nod].empty())
{w=v[nod].back();
v[nod].pop_back();
if(!viz[w])
{viz[w] = true;
if(muchie[w].a == nod)stack.push_back(muchie[w].b);
else stack.push_back(muchie[w].a);
}
}
else{
sol[++lg] = nod;
stack.pop_back();
}
}
for(i=1; i<lg; i++)
g<<sol[i]<<" ";
}