Cod sursa(job #2655950)

Utilizator Harsa_AndreiHarsa Andrei Harsa_Andrei Data 6 octombrie 2020 11:14:58
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;

list<int> M[50002];
queue<int> Q;
bool viz[50002];
int grad[50002];

int main()
{
    fin >> n >> m;
    int x, y;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        M[x].push_back(y);
        grad[y]++;
    }

    for(int i = 1; i <= n; i++)
        if(!grad[i])
        {
            Q.push(i);
            viz[i] = true;
        }

    while(!Q.empty())
    {
        int z = Q.front();
        Q.pop();
        fout << z << ' ';
        for(int t : M[z])
        {
            grad[t]--;
            if(!grad[t] && !viz[y])
            {
                Q.push(t);
                viz[t] = true;
            }
        }
    }
    return 0;
}