Cod sursa(job #3167861)

Utilizator Rux099Vasilescu Ruxandra Rux099 Data 11 noiembrie 2023 10:35:58
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

const int nmax = 50005;
const int mmax = 100005;
struct arc
{
    int x, y;
};

stack<int> St;
vector<arc> A[nmax];
int n, m, fr[nmax];

static inline void topo(int nod)
{
    while(A[nod].size())
    {
        arc k = A[nod].back();
        A[nod].pop_back();
        if(!fr[k.y])
            topo(k.y);
    }

    St.push(nod);
    fr[nod] = 1;
}

int main()
{
    f.tie();
    f >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int x, y;
        f >> x >> y;
        A[x].push_back({x, y});
    }

    for(int i = 1; i <= n; i ++)
        if(!fr[i])
            topo(i);

    while(!St.empty())
    {
        g << St.top() << " ";
        St.pop();
    }
    return 0;
}