Cod sursa(job #3268019)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 13 ianuarie 2025 15:32:19
Problema Ciclu Eulerian Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>

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

struct node {
    unordered_map<int,int> in;
    unordered_map<int,int> out;
};
array<node,500001> g;

vector<int> ans;
void solve(int x) {
    node& nodeX = g[x];
    while (!nodeX.out.empty()) {
        int y = nodeX.out.begin()->first;
        nodeX.out[y]--;
        if (!nodeX.out[y])
            nodeX.out.erase(nodeX.out.find(y));

        node& nodeY = g[y];
        nodeY.in[x]--;
        if (!nodeY.in[x])
            nodeY.in.erase(nodeY.in.find(x));

        solve(y);
    }
    ans.push_back(x);
}


int n,m;
int main() {
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x,y;
        fin >> x >> y;
        g[x].out[y]++;
        g[y].in[x]++;
    }

    solve(1);

    for (auto it = ans.rbegin(); it != ans.rend(); it++) {
        fout << *it << ' ';
    }
    return 0;
}