Cod sursa(job #2931566)

Utilizator CiuiGinjoveanu Dragos Ciui Data 31 octombrie 2022 15:21:13
Problema Ciclu Eulerian Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#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;
}