Pagini recente » Cod sursa (job #31660) | Cod sursa (job #2500283) | Cod sursa (job #115404) | Cod sursa (job #1693022) | Cod sursa (job #1982191)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, a[100][100], c[100];
void citire () {
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
a[x][y] = a[y][x] = 1;
}
}
void dfs (int nod) {
c[nod]++;
for (int i =1; i <= n; i++) {
if (a[i][nod] == 1 and c[i] == 0)
dfs(i);
}
}
int conex () {
dfs(1);
for (int i = 1; i <= n; i++) {
if (c[i] == 0)
return 0;
}
return 1;
}
int grad (int nod) {
int g = 0;
for (int i = 1; i <= n; i++) {
if (a[i][nod] == 1)
g++;
}
return g;
}
int eulerian () {
if (!conex())
return 0;
for (int i = 1; i <= n; i++) {
if (grad(i) % 2 == 1)
return 0;
}
return 1;
}
void ciclu (int nod) {
int maxx = 0, nmax = 0;
fout << nod << " ";
for (int i = 1; i <= n; i++) {
if (a[i][nod] == 1) {
if (grad(i) > maxx) {
maxx = grad(i);
nmax = i;
}
}
}
if (nmax != 0) {
a[nod][nmax] = a[nmax][nod] = 0;
ciclu(nmax);
}
}
int main () {
citire();
if (eulerian()) {
ciclu(1);
}
else {
fout << -1;
}
}