Pagini recente » Cod sursa (job #92678) | Cod sursa (job #1968903) | Cod sursa (job #1028378) | Cod sursa (job #2748512) | Cod sursa (job #1371001)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
const int N = 1e5 + 5;
int n, m;
vector <int> g[N], st;
int main() {
fin >> n >> m;
for (int x, y, i = 0; i < m; ++i) {
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; ++i)
if (g[i].size() & 1) {
fout << -1;
return 0;
}
int x = 0;
for (int i = 1; i <= n && !x; ++i)
if (g[i].size())
x = i;
st.push_back(x);
while (st.size()) {
int x = st.back();
if (!g[x].size()) {
fout << x << " ";
st.pop_back();
continue;
}
st.push_back(*g[x].begin());
int y = *g[x].begin();
swap(g[x][0], g[x].back());
g[x].pop_back();
vector <int> :: iterator it = g[y].begin();
while (*it != x)
++it;
swap(*it, g[y].back());
g[y].pop_back();
}
}