Cod sursa(job #1097135)

Utilizator mihai995mihai995 mihai995 Data 3 februarie 2014 01:03:00
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
using namespace std;

const int N = 50001;
vector<int> graph[N];
int ans[N], n;
bool use[N];

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

void dfs(int x){
    use[x] = true;
    for (vector<int> :: iterator it = graph[x].begin() ; it != graph[x].end() ; it++)
        if (!use[*it])
            dfs(*it);
    ans[ ++ans[0] ] = x;
}

int main(){
    int m, x, y;

    in >> n >> m;
    while (m--){
        in >> x >> y;
        graph[x].push_back(y);
    }

    for (int i = 1 ; i <= n ; i++)
        if (!use[i])
            dfs(i);

    for (int i = n ; i ; i--)
        out << ans[i] << " ";
    out << "\n";

    return 0;
}