Cod sursa(job #2824181)

Utilizator As932Stanciu Andreea As932 Data 31 decembrie 2021 13:05:34
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;
const int nmax = 50005;

int n, m;
int d[nmax];
vector <int> v[nmax], ans;

void read(){
    fin >> n >> m;

    for(int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
        d[y]++;
    }
}

void solve(){
    for(int i = 1; i <= n; i++)
        if(d[i] == 0)
            ans.push_back(i);

    for(int i = 0; i < n; i++){
        int x = ans[i];

        for(int j = 0; j < v[x].size(); j++){
            int y = v[x][j];
            d[y]--;
            if(d[y] == 0)
                ans.push_back(y);
        }
    }
}

void print(){
    for(int i = 0; i < n; i++)
        fout << ans[i] << " ";
}

int main()
{
    read();
    solve();
    print();

    return 0;
}