Pagini recente » Cod sursa (job #356152) | Cod sursa (job #422685) | Cod sursa (job #2934932) | Cod sursa (job #191850) | Cod sursa (job #2754673)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
vector<pair<int, int> > g[100005];
bool v[100005], ve[500005];
vector<int> cycle;
void dfs(int x) {
v[x] = true;
for(auto next: g[x])
if(!v[next.first])
dfs(next.first);
}
void euler(int x) {
while(g[x].size()) {
auto next = g[x].back();
g[x].pop_back();
if(ve[next.second]) continue;
ve[next.second] = true;
euler(next.first);
}
cycle.push_back(x);
}
int main() {
fin >> n >> m;
while(m--) {
int u, v;
fin >> u >> v;
g[u].push_back({v, m});
g[v].push_back({u, m});
}
dfs(1);
for(int i = 1; i <= n; i++)
if(g[i].size()&1 || !v[i]) {
fout << -1;
return 0;
}
euler(1);
for(auto x: cycle) fout << x << ' ';
}