Cod sursa(job #2572642)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 5 martie 2020 13:40:15
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define NMAX 50005

using namespace std;

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

queue < int > q;
vector < int > v[NMAX];
int t[NMAX];

int main()
{
    int n, m, x, y, i;

    fin >> n >> m;
    while ( m-- )
    {
        fin >> x >> y;
        v[x].push_back ( y );
        t[y]++;
    }

    for ( i = 1 ; i <= n ; i++ ) if ( t[i] == 0 ) q.push ( i );
    while ( q.empty() == 0 )
    {
        x = q.front();
        q.pop();

        for ( i = 0 ; i < v[x].size() ; i++ )
            if ( t[v[x][i]] == 1 ) q.push ( v[x][i] );
            else t[v[x][i]]--;

        fout << x << ' ';
    }

    return 0;
}