Cod sursa(job #2781462)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 9 octombrie 2021 17:03:57
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
#define NMAX 50005
#define pb push_back

using namespace std;

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

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

int main()
{
    vector < int > :: iterator it;
    int n, m, x, y, i;

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

    for(i = 1; i <= n; i++) if(t[i] == 0) a.push(i);

    while(a.empty() == 0)
    {
        x = a.front();
        a.pop();
        fout << x << ' ';

        for(it = v[x].begin(); it != v[x].end(); it++)
        {
            t[*it]--;
            if(t[*it] == 0) a.push(*it);
        }
    }

    return 0;
}