Pagini recente » Cod sursa (job #1768398) | Cod sursa (job #1623286) | Cod sursa (job #28477) | Cod sursa (job #2687175) | Cod sursa (job #2931566)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAX_ARCHES = 500005;
const int MAX_PEAKS = 100005;
vector<int> graph[MAX_ARCHES];
int from[MAX_PEAKS], to[MAX_PEAKS];
int isInGraph[MAX_ARCHES];
void findEuler(int curPeak) {
while (!graph[curPeak].empty()) {
int archNr = graph[curPeak].back();
graph[curPeak].pop_back();
if (isInGraph[archNr] == 0) {
isInGraph[archNr] = 1;
int start = from[archNr];
int end = to[archNr];
if (start == curPeak) {
findEuler(end);
} else {
findEuler(start);
}
}
}
if (curPeak != 1) {
fout << curPeak << " ";
}
}
int main() {
int peaks, arches;
fin >> peaks >> arches;
for (int i = 1; i <= arches; ++i) {
int start, end;
fin >> start >> end;
graph[start].push_back(i);
graph[end].push_back(i);
from[i] = start;
to[i] = end;
}
for (int i = 1; i <= peaks; ++i) {
if (graph[i].size() % 2 != 0) {
fout << -1;
fin.close();
fout.close();
return 0;
}
}
fout << 1 << " ";
findEuler(1);
fin.close();
fout.close();
return 0;
}