Cod sursa(job #2162151)

Utilizator CraiuAndrei Craiu Craiu Data 12 martie 2018 08:18:21
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nMax = 50005;
int N, M, top;
vector <int> L[nMax];
int viz[nMax], ans[nMax];

inline void Read()
{
    int x, y;
    fin >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
}

inline void Dfs(int node)
{
    viz[node] = 1;
    for(auto it : L[node])
        if(!viz[it])
            Dfs(it);
    ans[++top] = node;
}

int main()
{
    Read();
    for(int i = 1; i <= N; i++)
        if(!viz[i])
            Dfs(i);
    for(int i = N; i >= 1; i--)
        fout << ans[i] << " ";
    return 0;
}