Cod sursa(job #2496921)

Utilizator ArdeleanOficialAlexandru ArdeleanOficial Data 21 noiembrie 2019 20:39:55
Problema Ciclu Eulerian Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 1e5 + 7;

bool used[5 * N];
vector < int > adia[N];
vector < pair < int, int > > muchii;

int main()
{
    int n, m, a, b;
    fin >> n >> m;
    while (m--) {
        fin >> a >> b;
        adia[a].push_back(muchii.size());
        adia[b].push_back(muchii.size());
        muchii.push_back({a, b});
    }
    vector < int > stk;
    vector < int > ans;
    stk.push_back(1);
    while (stk.size()) {
        int nod = stk.back();
        while (adia[nod].size() && used[adia[nod].back()])
            adia[nod].pop_back();
        if (adia[nod].size()) {
            used[adia[nod].back()] = 1;
            stk.push_back(muchii[adia[nod].back()].first ^ muchii[adia[nod].back()].second ^ nod);
            adia[nod].pop_back();
        }
        else {
            ans.push_back(nod);
            stk.pop_back();
        }
    }
    for (int i = 0; i < ans.size() - 1; ++i)
        fout << ans[i] << ' ';
    return 0;
}