Cod sursa(job #1880219)

Utilizator JavaAlexDinu Alexandru JavaAlex Data 15 februarie 2017 17:00:36
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

vector<int> v[100000];
int n, m, stop[50000], nr;
bool viz[50000];

void citire(){
    ifstream r("sortaret.in");
    r >> n >> m;
    int i, x, y;
    for(i=0; i<m; i++){
        r >> x >> y;
        v[x].push_back(y);
    }
    r.close();
}

void dfs(int x){
    int i, y;
    viz[x] = true;
    for(i=0; i<v[x].size(); i++){
        y = v[x][i];
        if(!viz[y])
            dfs(y);
    }
    stop[++nr] = x;
}

int main()
{
    ofstream w("sortaret.out");
    int i;
    citire();
    for(i=1; i<=n; i++)
        if(!viz[i])
            dfs(i);
    for(i=1; i<=nr; i++)
        w<<stop[i]<<" ";
    return 0;
}