Cod sursa(job #1575598)

Utilizator serbanSlincu Serban serban Data 21 ianuarie 2016 17:51:27
Problema Sortare topologica Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

vector<int> ans;
vector<int> L[50100];
int c[50100];

int main()
{
    ifstream f("sortaret.in");
    ofstream g("sortaret.out");

    int n, m, x, y;
    f >> n >> m;
    for(int i = 1; i <= m; i ++) {
        f >> x >> y;
        c[y] ++;
        L[x].push_back(y);
    }

    bool ok = true;
    while(ok) {
        ok = false; int ies;
        for(int i = 1; i <= n; i ++) {
            if(c[i] == 0) ies = i, ok = true;
        }
        if(!ok) break;
        for(int i = 0; i < L[ies].size(); i ++) {
            c[L[ies][i]] --;
        }
        ans.push_back(ies);
        c[ies] = -1;
    }
    for(int i = 0; i < ans.size(); i ++) g << ans[i] << " ";
    g << "\n";
    return 0;
}