Pagini recente » Cod sursa (job #1513183) | Cod sursa (job #2701948) | Cod sursa (job #1520581) | Cod sursa (job #157640) | Cod sursa (job #1124092)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <deque>
#include <stack>
using namespace std;
ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");
const int nmax = 100000;
int n;
int grad[nmax + 2];
stack <int> st;
deque <int> dq;
vector <int> v[nmax + 2];
bool vizitat[nmax + 2];
void citeste () {
int m;
f >> n >> m;
int a, b;
for (int i = 1; i <= m; i++) {
f >> a >> b;
v[a].push_back (b);
v[b].push_back (a);
grad[a]++; grad[b]++;
}
//for (int i = 1; i <= n; i++) cout << grad[i] << ' ';
}
void DFS (int nod) {
vizitat[nod] = true;
int l = (int)v[nod].size ();
for (int i = 0; i < l; i++)
if (!vizitat[v[nod][i]]) DFS (v[nod][i]);
}
int OK () {
for (int i = 1; i <= n; i++)
if (!vizitat[i] || grad[i] % 2 == 1) return 0;
return 1;
}
void euler () {
st.push(1);
int nod, x;
while (!st.empty ()) {
nod = st.top ();
if (!v[nod].size ()) {
g << nod << ' ';
st.pop ();
}
else {
x = v[nod].back ();
st.push (x);
v[x].erase (find(v[x].begin (), v[x].end (), nod));
v[nod].pop_back ();
}
}
}
int main () {
citeste ();
DFS (1);
if (OK ()) euler ();
else g << -1;
g << '\n';
return 0;
}