Cod sursa(job #2418445)

Utilizator capmareAlexCapmare Alex capmareAlex Data 5 mai 2019 00:44:04
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
#define NMAX 50005
using namespace std;

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

int n,m;
vector< int > v[NMAX];
queue< int > q,q2;
int deg[NMAX];
int main()
{
    fin >> n >> m;
    while( m )
    {
        int x, y;
        fin >>x >>y;
        v[x].push_back(y);
        ++deg[y];
        --m;
    }
    for(int i = 1; i <= n; ++i)if(deg[i]==0)q.push(i);
    while(q.size())
    {
        int t=q.front();
        q.pop();
        q2.push(t);
        for(auto x : v[t])
        {
            --deg[x];
            if(!deg[x])q.push(x);
        }
    }
    while(q2.size())
    {
        fout<<q2.front()<<" ";
        q2.pop();
    }
    return 0;
}