Cod sursa(job #1856745)

Utilizator r00t_Roman Remus r00t_ Data 25 ianuarie 2017 13:11:02
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

vector<int>v1[50010],rez;
bool vf[50010];

void topSort(int x){
    vf[x]=1;

    for(int i=0;i<v1[x].size();i++){
        if(vf[v1[x][i]] == 0)
            topSort(v1[x][i]);
    }
    rez.push_back(x);
}

int main()
{
    int n,m,x,y;
    fin>>n>>m;
    for(int i =0;i<m;i++){
        fin>>x>>y;
        v1[x].push_back(y);
    }
    for(int i=1;i<=n;i++){
        if(vf[i]==0)
            topSort(i);
    }
    for(int i=rez.size()-1;i>=0;i--)
        fout<<rez[i]<<' ';
    return 0;
}