Cod sursa(job #2335138)

Utilizator TRIST0248SASASASA TRIST0248 Data 3 februarie 2019 17:18:34
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
#define x first
#define y second
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pi>
#define vpl vector<pl>
#define pb push_back
#define INF 1000000005
#define LINF 1000000000000000005
using namespace std;
int n, m;
int v[100001];
vector <int> g[100001];
queue <int> srt;
int tp(int nod){
    for(int i = 0 ; i < g[nod].size() ; i++){
        if(!v[g[nod][i]])tp(g[nod][i]);
    }
    v[nod] = 1;
    srt.push(nod);
}
int a,b;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    fin >> n >> m;
    for(int i = 1 ; i <= m; i++){
        fin >> a >> b;
        g[b].pb(a);
    }
    for(int i = 1 ; i <= n; i++){
        if(!v[i])tp(i);
    }
    while(!srt.empty()){
        fout << srt.front() << " ";
        srt.pop();
    }
}