Cod sursa(job #3352153)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 24 aprilie 2026 12:40:09
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int Nmax = 1e5+5,Mmax = 5e5+5;

int deg[Nmax], n, m, x, y;
bool Use[Nmax];
queue<int> Q;
vector <int> G[Nmax];

void read()
{
    fin>>n>>m;
    for(int i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        G[x].push_back(y);
        deg[y]++;
    }
}

void sort_top()
{
    for (int i = 1; i <= n; i++)
        if (deg[i] == 0)    ///grad interior
       {
            Q.push(i);
            Use[i] = 1;
        }
    while (!Q.empty())
    {
        x = Q.front();
        fout << x<<" ";
        for(auto it : G[x]) {
            if (!Use[it] && deg[it] == 1)
            {
                deg[it]--;
                Q.push(it);
                Use[it] = true;
            }
        }
        Q.pop();
    }
}
    int main()
    {
        read();
        sort_top();
        return 0;
    }