Cod sursa(job #2931989)

Utilizator CiuiGinjoveanu Dragos Ciui Data 1 noiembrie 2022 14:42:11
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#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;
}