Cod sursa(job #902096)

Utilizator Catah15Catalin Haidau Catah15 Data 1 martie 2013 12:50:11
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

#define PB push_back
#define maxN 50005

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


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

    for (int i = 0; i < List[node].size(); ++ i)
    {
        int node2 = List[node][i];

        if (Viz[node2]) continue;

        DFS (node2);
    }

    sol.PB (node);
}


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]) continue;
        DFS (i);
    }

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

    return 0;
}