Pagini recente » Cod sursa (job #328880) | Cod sursa (job #2499845) | Cod sursa (job #1219782) | Cod sursa (job #2659843) | Cod sursa (job #1267846)
#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, MMAX = 500000 + 1;
int n, m;
vector <int> graf[NMAX];
bool vizitat[MMAX], nod_vizitat[NMAX];
int grad[NMAX];
stack <int> stiva;
void euler(int nod) {
stiva.push(nod);
int fiu, l;
while (!stiva.empty()) {
nod = stiva.top();
l = graf[nod].size();
if (l == 0) {
g << nod << ' ';
stiva.pop();
}
else {
fiu = graf[nod][l - 1];
stiva.push(fiu);
graf[fiu].erase(find(graf[fiu].begin(), graf[fiu].end(), nod));
graf[nod].pop_back();
}
}
}
void citeste() {
f >> n >> m;
int a, b;
for (int i = 1; i <= m; i++) {
f >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
grad[a]++; grad[b]++;
}
}
bool grade_pare() {
for (int i = 1; i <= n; i++) if (grad[i] % 2 == 1) return false;
return true;
}
void DFS(int nod) {
nod_vizitat[nod] = true;
int l = graf[nod].size(), fiu;
for (int i = 0; i < l; i++) {
fiu = graf[nod][i];
if (!nod_vizitat[fiu]) DFS(fiu);
}
}
bool conex() {
DFS(1);
for (int i = 1; i <= n; i++)
if (!nod_vizitat[i]) return false;
return true;
}
void rezolva() {
if (grade_pare() && conex()) euler(1);
else g << "-1";
}
int main() {
citeste();
rezolva();
return 0;
}