Cod sursa(job #2629405)

Utilizator drknss_Hehe hehe drknss_ Data 20 iunie 2020 15:25:31
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 666013;
const int N = 2e5 + 11;
const int NMAX = 1e4 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

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

int n, m, viz[N], topo[N], k;

vector<int>v[N];

void dfs(int nod){

    viz[nod] = 1;

    for(auto it : v[nod]){
        if(!viz[it])dfs(it);
    }

    topo[++k] = nod;
}

void solve(){

    in >> n >> m;

    while(m--){

        int x, y;

        in >> x >> y;

        v[x].pb(y);
    }

    for(int i = 1; i <= n; i++){
        if(!viz[i])dfs(i);
    }

    for(int i = k; i > 0; i--){
        out << topo[i] << ' ';
    }
}

int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();

    //cout << setprecision(20) << fixed;

    int t = 1;
    //in >> t;
    while(t--){
        solve();
    }
}