Pagini recente » Cod sursa (job #867711) | Cod sursa (job #2558003) | Cod sursa (job #2120209) | Cod sursa (job #2563375) | Cod sursa (job #2170035)
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
const int MAXN = 100005;
int n, m, ciclu[5 * MAXN];
list<int> g[MAXN];
stack<int> st;
void remove_edge(int x, int y) {
g[x].erase(g[x].begin());
g[y].erase(find(g[y].begin(), g[y].end(), x));
}
int main()
{
in >> n >> m;
for (int i = 1, x, y; i <= m; ++i) {
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; ++i) {
if (g[i].size() % 2 != 0) {
out << -1; return 0;
}
}
st.push(1);
while (!st.empty()) {
int x = st.top();
if (g[x].size() == 0) {
ciclu[++ciclu[0]] = x;
st.pop();
continue;
}
int y = *g[x].begin();
st.push(y);
remove_edge(x, y);
}
for (int i = ciclu[0]; i >= 1; --i) out << ciclu[i] << " ";
return 0;
}