Cod sursa(job #2816897)

Utilizator alexxxxxxhalex alx alexxxxxxh Data 12 decembrie 2021 14:05:51
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector <vector<int>>v;
int b[50001];int n,m;
void f()
{

    queue <int> Q;
    for (int l=1;l<=n;l++) if (b[l]==0) Q.push(l);

    while (!Q.empty())
    {
        int i=Q.front();
        Q.pop();
fout << i <<" ";
        for (int j=0;j<v[i].size();j++)
        {
            b[v[i][j]]--;
            if (b[v[i][j]]==0)
            {
                Q.push(v[i][j]);
            }
        }
    }


}
int main()
{

    fin >>n>>m;
    v.resize(50001);
    for (int i=1; i<=m; i++)
    {
        int x,y;
        fin >>x>>y;
        v[x].push_back(y);
        b[y]++;
    }
f();
}