Cod sursa(job #3167850)

Utilizator Rux099Vasilescu Ruxandra Rux099 Data 11 noiembrie 2023 10:24:39
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 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[mmax];

static inline sorttop(int nod)
{
    while(A[nod].size())
    {
        arc k = A[nod].back();
        A[nod].pop_back();
        sorttop(k.y);
    }

    St.push(nod);
}

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});
    }

    sorttop(1);

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