Pagini recente » Cod sursa (job #712186) | Cod sursa (job #1573802) | Cod sursa (job #2479220) | Cod sursa (job #2262553) | Cod sursa (job #2106341)
#include <bits/stdc++.h>
#define N 100100
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
int n, m, vf, x, y, stk[10 * N];
vector <pair <int, int> > v[N];
pair <int, int> nod;
vector <int> sol;
bool viz[5 * N];
int main(){
in >> n >> m;
for(int i = 1; i <= m; i++){
in >> x >> y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
for(int i = 1; i <= n; i++)
if(v[i].size() & 1)
return out << -1, 0;
stk[++vf] = 1;
while(vf){
x = stk[vf];
if(v[x].empty()){
sol.push_back(x);
vf--;
continue;
}
nod = v[x].back();
v[x].pop_back();
if(!viz[nod.second]){
viz[nod.second] = 1;
stk[++vf] = nod.first;
}
}
for(auto i : sol)
out << i << ' ';
return 0;
}