Cod sursa(job #2613370)

Utilizator Misha22Misha S Misha22 Data 9 mai 2020 16:33:23
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/

#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);

#define in fin
#define out fout

using namespace std;

typedef long long ll;
typedef unsigned int uint;
const int nmax = 300005;
const ll linf = LLONG_MAX;
const int mod = 1e9+7;
const int inf = INT_MAX;

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

int n, m, indg[500005];
vec <int> gf[500005];
queue <int> ord;
int main(){
    int a, b;
    in >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> a >> b;
        gf[a].pb(b);
        indg[b] ++;
    }
    for(int i = 1; i <= n; i++){
        if(indg[i] == 0)ord.push(i);
    }
    while(!ord.empty()){
        int top = ord.front();
        ord.pop();
        out << top << ' ';
        for(int st : gf[top]){
            indg[st]--;
            if(indg[st] == 0)ord.push(st);
        }
    }
}