Pagini recente » Cod sursa (job #1451636) | Cod sursa (job #3041032) | Cod sursa (job #357640) | Cod sursa (job #355763) | Cod sursa (job #1898409)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
int n, m;
vector<int> g[100001];
stack<int> st;
bool eulerian() {
for (int i = 1; i <= n; ++i)
if (g[i].size() % 2 != 0) return false;
return true;
}
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() {
int x;
in >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if (!eulerian()) {
out << -1;
return 0;
}
st.push(1);
while (!st.empty()) {
x = st.top();
while(!g[x].empty()) {
int y = g[x][0];
remove_edge(x, y);
st.push(y);
x = y;
}
if (st.size() >= 2) out << st.top() << " ";
st.pop();
}
return 0;
}