Pagini recente » Cod sursa (job #1202745) | Cod sursa (job #1177002) | Cod sursa (job #1060070) | Cod sursa (job #1721528) | Cod sursa (job #1870678)
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())
using namespace std;
const int NMAX = 100000, MMAX = 500000;
vector<int> G[NMAX];
int E[MMAX];
int main(void) {
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y; x--; y--;
E[i] = x ^ y;
G[x].push_back(i);
G[y].push_back(i);
}
for (int i = 0; i < n; i++) {
if (SZ(G[i]) & 1) {
cout << "-1\n";
return 0;
}
}
vector<int> stk;
stk.push_back(0);
while (not stk.empty()) {
const int node = stk.back();
while (not G[node].empty() and E[G[node].back()] == -1) {
G[node].pop_back();
}
if (G[node].empty()) {
stk.pop_back();
if (not stk.empty()) {
cout << node + 1 << ' ';
}
} else {
stk.push_back(E[G[node].back()] ^ node);
E[G[node].back()] = -1;
G[node].pop_back();
}
}
}