123123

 avatar
user_8384735
c_cpp
2 years ago
580 B
4
Indexable
Never
// P3916.cpp
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e5 + 5;
vector<vector<int> > G(N, vector<int>());
bool vis[N];
int ans[N];
int maxi;
int m, n, cnt;
bool used[N];

void dfs(int x, int t){
	if (!ans[x]) ans[x] = t;
	vis[x] = 1;
	int res = x;
	for (int y : G[x]){
		if (!vis[y]){
			dfs(y,t);
		}
	}
}
int main(){
	cin >> m >> n;
	while (n--){
		int u,v;
		cin >> u >> v;
		G[v].push_back(u);
	}
	for (int i = m; i >= 1; i--){
		if(!ans[i])
			dfs(i, i);
	}
	for (int i = 1; i <= m; i++) cout << ans[i] << " ";
}