Cod sursa(job #2570551)

Utilizator BenjaminOnlyBenjamin Popescu BenjaminOnly Data 4 martie 2020 17:33:18
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,x,y,i,intr[50001];
vector<vector<int>> v;
stack <int> stk;

int main()
{
    f >> n >> m;
    v.resize(n + 1);
    while(m --)
    {
        f >> x >> y;
        v[x].push_back(y);
        ++ intr[y];
    }
    for(i = 1; i <= n; ++ i)
        if(!intr[i])
            stk.push(i);
    while(!stk.empty())
    {
        x = stk.top();
        stk.pop();
        g << x << ' ';
        for(i = 0; i < v[x].size(); ++ i)
        {
            -- intr[v[x][i]];
            if(!intr[v[x][i]])
                stk.push(v[x][i]);
        }
    }
}