Cod sursa(job #2356971)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 27 februarie 2019 01:11:01
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>
#define dimm 50005
#define dimn 100005

using namespace std;

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


int n, i, j, m, nr[dimn];
vector < int > v[dimm];
queue < int > Q;


void topsort()
{
    for ( i = 1  ; i<=n; i++)
    if ( nr[i] == 0)
        Q.push(i);

    while( !Q.empty())
    {
        int old_node = Q.front();
        Q.pop();
        g << old_node << " ";

        int l = v[old_node].size();

        for ( i = 0; i < l; i++ )
        {
            int new_node = v[old_node][i];
            nr[new_node]--;
            if ( nr[new_node] == 0 )
            Q.push(new_node);
        }
    }
}

int main()
{
    f >> n >> m;
    while ( m --)
    {
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
        nr[y]++;
    }
    topsort();
    return 0;
}