Pagini recente » Cod sursa (job #2223962) | Cod sursa (job #2292428) | Cod sursa (job #967377) | Cod sursa (job #2830480) | Cod sursa (job #3030919)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int N = 1e5 + 5;
const int M = 5e5 + 5;
int n, m;
vector<int> stk, rez, v[N];
vector<pair<int, int>> muchii;
bitset<M> u;
void read(), check(), euler();
int main()
{
read();
check();
euler();
rez.pop_back();
for (auto it: rez) g << it << ' ' ;
return 0;
}
void read(){
f >> n >> m;
int x, y;
for (int i = 0; i < m; i++){
f >> x >> y;
v[x].pb(i); v[y].pb(i);
muchii.pb({x, y});
}
}
void check(){
for (int i = 1; i <= n; i++)
if (v[i].size() % 2){
g << -1; exit(0);
}
}
void euler(){
stk.pb(1);
while (stk.size()){
int nod = stk.back();
if (v[nod].size()) {
int ord = v[nod].back(); v[nod].pop_back();
if (u[ord]) continue;
u[ord] = 1;
int x, y; tie(x, y) = muchii[ord];
x == nod ? stk.pb(y) : stk.pb(x);
}
else {
stk.pop_back();
rez.pb(nod);
}
}
}