Pagini recente » Cod sursa (job #2904486) | Cod sursa (job #1646575) | Cod sursa (job #2573193) | Cod sursa (job #1336854) | Cod sursa (job #1240443)
#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 viz[NMAX];
stack <int> st;
vector <int> graf[NMAX];
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) {
viz[nod] = 1;
int l = graf[nod].size();
for (int i = 0; i < l; i++)
if (!viz[graf[nod][i]]) DFS(graf[nod][i]);
}
bool OK() {
for (int i = 1; i <= n; i++)
if (graf[i].size() % 2 == 1 || !viz[i]) return false;
return true;
}
void euler() {
int x, nod;
st.push(1);
while (!st.empty()) {
nod = st.top();
if (graf[nod].size() == 0) {
g << nod << ' ';
st.pop();
}
else {
x = graf[nod][graf[nod].size() - 1];
st.push(x);
graf[x].erase(find(graf[x].begin(), graf[x].end(), nod));
graf[nod].pop_back();
}
}
}
int main() {
citeste();
DFS(1);
if (OK()) {
euler();
}
else {
g << "-1\n";
}
}