Cod sursa(job #2878765)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 27 martie 2022 16:37:24
Problema Sortare topologica Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()

/*const ll maxn = 1e5;
int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
    f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;} */

const ll mod = 1e9+7;
ll n, k, m, mi, ma;
const ll N = 2e5;


vector<ll> a[N];
vector<ll> v(N, 0), ans;

bool top_sort(ll x){
	v[x] = 1;
	for(int i = 0; i<a[x].size(); i++){
		ll r = a[x][i];
		if(v[r] == 1) return 1;	
		bool u = top_sort(r);
		if(u) return 1;
	}
	v[x] = 2;
	ans.pb(x);
	return 0;
}


void solve(){

	ifstream cin("sortaret.in");
	ofstream cout("sortaret.out");
	
	cin >> n >> m;
	for(int i = 0; i<n; i++){
		a[i].clear();
		v[i] = 0;
	}
	ans.clear();
	for(int i = 0; i<m; i++){
		ll x, y;
		cin >> x >> y;
		x--; y--;
		a[x].pb(y);
	}
	for(int i = 0; i<n; i++) if(v[i] == 0){
		bool u = top_sort(i);
		if(u == 0) continue;
		cout << "NO\n";
		return; 
	}
	reverse(all(ans));
	for(int i = 0; i<n; i++) cout << ans[i] + 1 << " "; cout << "\n";
	

}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	//init();
	
	int t=1;
//	cin >> t;
	while(t--) solve();
	
	return 0;
}