Cod sursa(job #874404)

Utilizator Catah15Catalin Haidau Catah15 Data 8 februarie 2013 12:14:39
Problema Sortare topologica Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

#define maxN 50005
#define PB push_back

vector <int> List[maxN], sol;
bool Viz[maxN];


void DFS (int nod)
{
    Viz[nod] = true;

    for (int i = 0; i < List[nod].size(); ++ i) DFS (List[nod][i]);

    sol.PB (nod);
}


int main()
{
    freopen ("sortaret.in", "r", stdin);
    freopen ("sortaret.out", "w", stdout);

    int N, M;
    scanf ("%d %d", &N, &M);

    while (M --)
    {
        int x, y;
        scanf ("%d %d", &x, &y);

        List[x].PB (y);
    }

    for (int i = 1; i <= N; ++ i) if (! Viz[i]) DFS (i);

    for (int i = sol.size() - 1; i >= 0; -- i) printf ("%d ", sol[i]);

    return 0;
}