Pagini recente » Cod sursa (job #2345144) | Cod sursa (job #1510905) | Cod sursa (job #736125) | Cod sursa (job #3152650) | Cod sursa (job #2931989)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAX_ARCHES = 500000;
vector<pair<int, int>> graph[MAX_ARCHES + 1];
bitset<MAX_ARCHES + 1> isInGraph;
void findEuler() {
vector<int> eulerPeaks, st;
st.push_back(1);
while (!st.empty()) {
int curPeak = st.back();
if (graph[curPeak].empty()) {
st.pop_back();
eulerPeaks.push_back(curPeak);
continue;
}
int archNr = graph[curPeak].back().second;
int nextPeak = graph[curPeak].back().first;
graph[curPeak].pop_back();
if (isInGraph[archNr] == 0) {
isInGraph[archNr] = 1;
st.push_back(nextPeak);
}
}
for (int i = 0; i < eulerPeaks.size() - 1; ++i) {
fout << eulerPeaks[i] << " ";
}
}
int main() {
int peaks, noArches;
fin >> peaks >> noArches;
for (int i = 1; i <= noArches; ++i) {
int start, end;
fin >> start >> end;
graph[start].push_back(make_pair(end, i));
graph[end].push_back(make_pair(start, i));
}
for (int i = 1; i <= peaks; ++i) {
if (graph[i].size() % 2 != 0) {
fout << -1;
return 0;
}
}
findEuler();
return 0;
}