Pagini recente » Cod sursa (job #850929) | Cod sursa (job #2506218) | Cod sursa (job #2821977) | Cod sursa (job #1925161) | Cod sursa (job #1870632)
#include <fstream>
#include <vector>
#define SZ(x) ((int)(x).size())
using namespace std;
const int NMAX = 100000;
const int MMAX = 500000;
vector<int> G[NMAX];
int E[MMAX];
void dfs(const int& node, vector<int>& cycle) {
while (not G[node].empty()) {
const int idx = G[node].back();
G[node].pop_back();
if (E[idx] != -1) {
const int to = E[idx] ^ node;
E[idx] = -1;
cycle.push_back(node);
cycle.push_back(to);
dfs(to, cycle);
}
}
}
int main() {
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
fin.tie(0);
ios_base::sync_with_stdio(false);
int n, m; fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y; fin >> 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) {
fout << "-1\n";
return 0;
}
}
vector<int> ans;
dfs(0, ans);
fout << 1 + ans.front();
for (int i = 1; i < SZ(ans); i += 2) {
fout << ' ' << 1 + ans[i];
}
fout << '\n';
return 0;
}