Pagini recente » Cod sursa (job #620102) | Cod sursa (job #620793) | Cod sursa (job #1011534) | Cod sursa (job #603858) | Cod sursa (job #2402172)
#include <fstream>
#include <vector>
#include <stack>
#include <unordered_set>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
using US = unordered_multiset<int>;
using VUS = vector<US>;
using VI = vector<int>;
int n;
VUS G;
VI c;
void citesteGraf();
inline void detCiclu(int x);
bool esteEulerian();
int main() {
citesteGraf();
if ( !esteEulerian() ) {
fout << -1;
} else {
detCiclu(1);
for (size_t i = 0; i < c.size() - 1; ++i)
fout << c[i] << ' ';
}
}
int x, y, m;
inline void detCiclu(int x) {
stack<int> stk;
stk.push(x);
while (!stk.empty()) {
x = stk.top();
if (G[x].empty()) {
stk.pop();
c.emplace_back(x);
} else {
auto it = G[x].begin(); y = *it;
G[x].erase(it);
it = G[y].find(x);
if (it != G[y].end())
G[y].erase(it);
stk.push(y);
}
}
}
bool esteEulerian() {
for (auto ms : G)
if (ms.size() & 1)
return false;
return true;
}
void citesteGraf() {
fin >> n >> m;
G = VUS(n + 1);
while (m--) {
fin >> x >> y;
G[x].emplace(y);
G[y].emplace(x);
}
}