Cod sursa(job #2172152)

Utilizator RickSanchezRick Sanchez RickSanchez Data 15 martie 2018 15:18:29
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int nmax = 50005;
int N,M,K;
int st[nmax],viz[nmax];
vector < int > L[nmax];

inline void DFS(int node)
{
    viz[node] = true;
    for(auto it : L[node])
        if(!viz[it]) DFS(it);
    st[++K] = node;
}



int main()
{
    int i,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]) DFS(i);
    for(i = N; i >= 1; i--) fout << st[i] << " ";
    return 0;
}