Pagini recente » Cod sursa (job #1449445) | Cod sursa (job #362953) | Cod sursa (job #410184) | Cod sursa (job #1086052) | Cod sursa (job #2170192)
#include <iostream>
#include <fstream>
#include <list>
#include <stack>
#include <algorithm>
#define maxN 100005
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, Cycle[maxN], nr;
list <int> G[maxN];
stack <int> st;
void Remove (int x, int y) {
G[x].erase(G[x].begin());
G[y].erase(find(G[y].begin(), G[y].end(), x));
}
bool EulCheck () {
for (int i = 1; i <= n; ++i) {
if (G[i].size() & 1)
return false;
}
return true;
}
void EulerUtil () {
st.push(1);
while (!st.empty()) {
int x = st.top();
if (G[x].size() == 0) {
Cycle[++nr] = x;
st.pop();
continue;
}
int y = *G[x].begin();
st.push(y);
Remove(x, y);
}
}
int main () {
fin >> n >> m;
int x, y;
for (int i = 1; i <= m; ++i) {
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
if (not EulCheck()) {
cout << - 1;
return 0;
}
EulerUtil();
for (int i = nr; i >= 1; --i) {
fout << Cycle[i] << " ";
}
}