Cod sursa(job #2652551)

Utilizator rohitrjRohit kumar rohitrj Data 25 septembrie 2020 09:15:11
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define prec(n) fixed<<setprecision(n)
// declaration shortcuts
typedef long long int ll;
#define int ll
// Constants
constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1};
constexpr ll INF = 1999999999999999997; 
constexpr int inf= INT_MAX;
constexpr int MAXSIZE = int(1e6)+5;
constexpr auto PI  = 3.14159265358979323846L;
constexpr auto eps = 1e-6;
constexpr auto mod = 1000000007;
constexpr auto maxn = 100006;
void IOfile(){
freopen("sortaret.in", "r", stdin);
freopen("sortaret.out", "w", stdout);
}
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}

vector<int>v[maxn];
vector<int>ans;
int visited[maxn];
int in[maxn];
void dfs(int st){
	visited[st] = 1;
	for(auto i:v[st]){
		if(!visited[i]) dfs(i);
	}
	ans.push_back(st);
}

int32_t main(){
	fastio();
	//IOfile()
	int n,m;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a,b;
		cin >>a >> b;
		v[a].push_back(b);
		in[b]++;

	}
	for(int i = 1; i <= n; i++){
		if(!visited[i] and in[i] == 0){
			dfs(i);
		}
	}
	reverse(all(ans));
	for(auto i:ans) cout << i << " ";
}