Pagini recente » Cod sursa (job #1242842) | Cod sursa (job #1235928) | Cod sursa (job #2844085) | Cod sursa (job #1808091) | Cod sursa (job #1243759)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream f ("ciclueuler.in");
ofstream g ("ciclueuler.out");
const int NMAX = 100000 + 1;
int n, m;
bool vizitat[NMAX];
vector <int> graf[NMAX];
stack <int> st;
void citeste() {
int a, b;
f >> n >> m;
for (int i = 1; i <= m; i++) {
f >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
}
void DFS(int nod) {
vizitat[nod] = true;
int l = graf[nod].size();
for (int i = 0; i < l; i++)
if (!vizitat[graf[nod][i]]) DFS(graf[nod][i]);
}
bool OK() {
DFS(1);
for (int i = 1; i <= n; i++) if (graf[i].size() % 2 == 1 || !vizitat[i]) return false;
return true;
}
void euler() {
st.push(1);
int nod, fiu, l;
while (!st.empty()) {
nod = st.top();
l = graf[nod].size();
if (l == 0) g << nod << ' ', st.pop();
else {
fiu = graf[nod][l - 1];
st.push(fiu);
graf[fiu].erase(find(graf[fiu].begin(), graf[fiu].end(), nod));
graf[nod].pop_back();
}
}
}
int main() {
citeste();
if (!OK()) {
g << "-1\n";
return 0;
}
else euler();
}