Cod sursa(job #1294030)

Utilizator japjappedulapPotra Vlad japjappedulap Data 16 decembrie 2014 21:45:08
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is ("sortaret.in");
ofstream os ("sortaret.out");

int N, M;
bool B[50005];
vector <int> G[50005];
int TP[50005], Nr;

void DFS(int x);

int main()
{
    is >> N >> M;
    for (int i = 1, a, b; i <= M; ++i)
    {
        is >> a >> b;
        G[a].push_back(b);
    }
    Nr = N;
    for (int i = 1; i <= N; ++i)
        if (B[i] == 0)
            DFS(i);
    for (int i = 1; i <= N; ++i)
        os << TP[i] << ' ';
    is.close();
    os.close();
}

void DFS(int x)
{
    B[x] = 1;
    for (const auto& y : G[x])
        if (B[y] == 0)
            DFS(y);
    TP[Nr] = x; Nr--;
};