Pagini recente » Cod sursa (job #2159889) | Cod sursa (job #1853638) | Cod sursa (job #2724003) | Cod sursa (job #3168665) | Cod sursa (job #2416251)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
#define nmax 100005
#define mmax 500005
vector<int> v[nmax], sol;
int n, m, to[mmax], from[mmax], x, y;
bool u[mmax];
int main()
{
f >> n >> m;
for (int i = 0; i < m; ++i){
f >> x >> y;
v[x].push_back(i), v[y].push_back(i);
from[i] = x;
to[i] = y;
}
for (int i = 1; i <= n; ++i)
if(v[i].size() & 1){ g << "-1\n"; return 0; }
stack<int> q;
q.push(1);
while(!q.empty()){
int nod = q.top();
if(v[nod].size() == 0){
q.pop();
sol.push_back(nod);
}
else {
int e = v[nod].back();
v[nod].pop_back();
if(!u[e]){
u[e] = 1;
int qnxt = from[e];
if(nod == from[e])
qnxt = to[e];
q.push(qnxt);
}
}
}
for (unsigned int i = 0; i < sol.size() - 1; ++i) g << sol[i] << " ";
return 0;
}