Pagini recente » Clasament Seniori | Cod sursa (job #461118) | Statistici Patrascu Gabriel Alexandru (Patraxbi) | Cod sursa (job #3198640) | Cod sursa (job #2864518)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using VP = vector<PII>;
using VB = vector<bool>;
VVI g;
VI ciclu;
VB erased;
VP muchie;
vector<vector<int>::iterator> ramas;
int n, m;
inline void Euler(int const& start) {
stack<int> stiva;
stiva.emplace(start);
while (!stiva.empty()) {
int x = stiva.top(), to_push = 0;
for (; ramas[x] != g[x].end(); ++ramas[x]) {
int ind = *ramas[x];
if (!erased[ind]) {
erased[ind] = 1;
int y = muchie[ind].first;
if (x == y)
y = muchie[ind].second;
to_push = y;
break;
}
}
if (!to_push) {
stiva.pop();
ciclu.emplace_back(x);
}
else stiva.emplace(to_push);
}
}
int main() {
fin >> n >> m;
g = VVI(n + 1);
muchie = VP(m + 1);
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
muchie[i] = {x, y};
g[x].emplace_back(i);
g[y].emplace_back(i);
}
ramas.resize(n + 1);
for (int i = 1; i <= n; ++i) {
ramas[i] = g[i].begin();
if (g[i].size() % 2) {
fout << -1;
return 0;
}
}
erased = VB(m + 1);
Euler(1);
ciclu.pop_back();
for (int const& x : ciclu)
fout << x << ' ';
fin.close();
fout.close();
return 0;
}