# Cod sursa(job #1789307)

Utilizator Data 26 octombrie 2016 21:05:52 Ciclu Eulerian 100 cpp done Arhiva educationala 1.25 kb
``````#include <algorithm>
#include <fstream>
#include <vector>
#define SZ(x) ((int) (x).size())
using namespace std;

const int NMAX = 100005, MMAX = 500005;

vector<int> G[NMAX];

bool usedEdge[MMAX];
int from[MMAX], to[MMAX];

int main() {
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int n, m;
fin >> n >> m;

for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
G[x].push_back(i);
G[y].push_back(i);
from[i] = x;
to[i] = y;
}

for (int i = 1; i <= n; ++i) {
if (SZ(G[i]) & 1) {
fout << "-1\n";
return 0;
}
}

vector<int> ans;

vector<int> stk;
stk.push_back(1);
while (!stk.empty()) {
int node = stk.back();
if (!G[node].empty()) {
int e = G[node].back();
G[node].pop_back();
if (!usedEdge[e]) {
usedEdge[e] = true;
int to = ::from[e] ^ ::to[e] ^ node;
stk.push_back(to);
}
} else {
stk.pop_back();
ans.push_back(node);
}
}

for (int i = 0; i < SZ(ans) - 1; ++i) {
fout << ans[i] << ' ';
}
fout << '\n';

fin.close();
fout.close();
}
``````