Cod sursa(job #2699645)

Utilizator tomaionutIDorando tomaionut Data 25 ianuarie 2021 12:44:24
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, k;
vector <int> L[50005];
int viz[50005], sol[50005];
void Dfs(int x)
{
    viz[x] = 1;
    for (int i : L[x])
        if (viz[i]==0)
        Dfs(i);
    sol[++k] = x;
}
int main()
{
    int i, j,x,y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            Dfs(i);
    for (i = n; i >= 1; i--)
        fout << sol[i] << " ";

    return 0;
}