Cod sursa(job #1380266)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 7 martie 2015 05:07:04
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include    <fstream>
#include    <iostream>
#include    <vector>

using namespace std;

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

const int NLIM = 50005;
bool beenThere[NLIM];
int N, M, Answer[NLIM], k = 0;

vector < int > Edge[NLIM];

void DFS(int node)
{
    beenThere[node] = true;
    for(unsigned int i = 0; i < Edge[node].size(); i++)
    {
        if(!beenThere[Edge[node][i]])
            DFS(Edge[node][i]);
    }
    Answer[++k] = node;
}

void Read()
{
    int x, y;
    fin >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        fin >> x >> y;
        Edge[x].push_back(y);
    }
    for(int i = 1; i <= N; i++)
    {
        if(!beenThere[i])
            DFS(i);
    }
    for(int i = k; i >= 1; i--)
        fout << Answer[i] << " ";
    fout << "\n";
}

int main()
{
    Read();
    return 0;
}