Cod sursa(job #2632341)

Utilizator bem.andreiIceman bem.andrei Data 2 iulie 2020 21:11:24
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;
ifstream r("sortaret.in");
ofstream w("sortaret.out");
int n, m, sun[50007];
vector < int > v[50007];
queue < int > q;
int main()
{
    r>>n>>m;
    for (int i = 1 ; i <= m ; i ++)
    {
        int x, y;
        r>>x>>y;
        v[x].push_back (y);
        sub[y]++;
    }
    for (int i = 1 ; i <= n ; i ++)
        if ( sub[i] == 0 )
        {
            q.push (i);
        }
    while (q.size()!=0)
    {
        int x = q.front ();
        q.pop ();
        w<<x<<" ";
        for (int y = 0 ; y < v[x].size () ; y ++)
        {
            --sub[v[x][y]];
            if ( sub[v[x][y]] == 0 )
            {
                q.push ( v[x][y] );
            }
        }
    }
    return 0;
}