Pagini recente » Cod sursa (job #42093) | Cod sursa (job #2112689) | Cod sursa (job #930269) | Cod sursa (job #3123455) | Cod sursa (job #2712622)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 100005, mmax = 500005;
int n, m, from[mmax], to[mmax];
bool viz[mmax];
vector <int> G[nmax];
int main(){
fin >> n >> m;
for (int i = 1; i <= m; ++i){
int x, y;
fin >> x >> y;
G[x].push_back(i);
G[y].push_back(i);
from[i] = x;
to[i] = y;
}
bool euler = true;
for (int i = 1; i <= n; ++i){
if (G[i].size() & 1){
euler = false;
break;
}
}
if (!euler){
fout << -1;
return 0;
}
vector <int> s, ans;
s.push_back(1);
while (s.size() > 0){
int nod = s.back();
if (G[nod].size() > 0){
int muchie = G[nod].back();
G[nod].pop_back();
if (viz[muchie] == false){
viz[muchie] = true;
s.push_back(from[muchie] + to[muchie] - nod);
}
}
else{
ans.push_back(nod);
s.pop_back();
}
}
ans.pop_back();
for (auto it : ans){
fout << it << " ";
}
return 0;
}