Cod sursa(job #3315671)

Utilizator Allie28Radu Alesia Allie28 Data 15 octombrie 2025 17:23:11
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int LMAX = 50005;

vector<int> L[LMAX], LT[LMAX];
int top[LMAX], idx = 1;
bool viz[LMAX];

void dfs(int node) {
    viz[node] = 1;
    for (int vec : LT[node]) {
        if (!viz[vec]) {
            dfs(vec);
        }
    }
    top[idx] = node;
    idx++;
}


int main(){
    int n, m;
    fin>>n>>m;
    while (m--) {
        int x, y;
        fin>>x>>y;
        L[x].push_back(y);
        LT[y].push_back(x);
    }
    for (int i = 1; i <= n; i++) {
        if (!viz[i]) dfs(i);
    }
    for (int i = 1; i <= n; i++) {
        fout<<top[i]<<" ";
    }


    fin.close();
    fout.close();
    return 0;
}