Pagini recente » Cod sursa (job #2483155) | Cod sursa (job #2051442) | Cod sursa (job #819961) | Cod sursa (job #782111) | Cod sursa (job #2080553)
#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;
const int MMAX = 500000;
int n;
bool vizitat[NMAX + 1];
bool pus[MMAX + 1];
vector < pair <int, int> > graf[NMAX + 1];
stack <int> stiva;
void citeste() {
int e, a, b;
f >> n >> e;
for (int i = 1; i <= e; i++) {
f >> a >> b;
graf[a].push_back(make_pair(b, i));
graf[b].push_back(make_pair(a, i));
}
}
bool sunt_grade_pare() {
for (int i = 1; i <= n; i++)
if (graf[i].size() % 2) return false;
return true;
}
void DFS(int nod) {
if (vizitat[nod]) return;
vizitat[nod] = true;
int l = graf[nod].size();
for (int i = 0; i < l; i++)
DFS(graf[nod][i].first);
}
bool e_conex() {
DFS(1);
for (int i = 1; i <= n; i++)
if (!vizitat[i]) return false;
return true;
}
void euler() {
int nod, l, fiu;
stiva.push(1);
while (!stiva.empty()) {
nod = stiva.top();
l = graf[nod].size();
while(l > 0 && pus[graf[nod][l - 1].second]) {
graf[nod].pop_back();
l--;
}
if (l == 0) {
g << nod << ' ';
stiva.pop();
continue;
}
fiu = graf[nod][l - 1].first;
pus[graf[nod][l - 1].second] = true;
graf[nod].pop_back();
stiva.push(fiu);
}
}
int main() {
citeste();
if (sunt_grade_pare() && e_conex()) {
euler();
g << '\n';
}
else {
g << "-1\n";
}
return 0;
}