Cod sursa(job #2217779)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 2 iulie 2018 10:51:15
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("sortaret.in");
ofstream out("sortaret.out");

vector<int> v[50005], sol;
bool state[50005];

void dfs(int x)
{
    state[x] = 1;
    for (auto it: v[x])
        if (state[it] == 0)
            dfs(it);
    sol.push_back(x);
}

int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=n; i++)
    {
        int x,y;
        in >> x >> y;
        v[x].push_back(y);
    }
    for (int i = 1; i<=n; i++)
        if (state[i] == 0)
            dfs(i);
    reverse(sol.begin(),sol.end());
    for (auto it: sol)
        out << it << " ";
}