Pagini recente » Cod sursa (job #274692) | Cod sursa (job #489459) | Cod sursa (job #2084851) | Cod sursa (job #1828266) | Cod sursa (job #2653590)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 100005;
int n, m, ans[5 * nmax], z, from[5 * nmax], to[5 * nmax];
bool viz[5 * nmax];
vector <int> G[nmax], st;
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 ciclu_eulerian = true;
for (int i = 1; i <= n; ++i){
if (G[i].size() % 2 == 1){
ciclu_eulerian = false;
break;
}
}
if (ciclu_eulerian == false){
fout << -1 << "\n";
}
st.push_back(1);
while (!st.empty()){
int nod = st.back();
if (G[nod].size() > 0){
int nod = st.back();
int e = G[nod].back();
G[nod].pop_back();
if (viz[e] == false){
viz[e] = true;
int vecin = from[e] + to[e] - nod;
st.push_back(vecin);
}
}
else{
ans[++z] = st.back();
st.pop_back();
}
}
for (int i = 1; i < z; ++i){
fout << ans[i] << " ";
}
fin.close();
fout.close();
return 0;
}