Cod sursa(job #2356970)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 27 februarie 2019 01:09:23
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 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[i]++;
    }
    topsort();
    return 0;
}