P3916

 avatar
user_8384735
c_cpp
2 years ago
655 B
5
Indexable
// 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];

int dfs(int x){
	//if (maxi == m) return maxi;
	vis[x] = 1;
	int res = x;
	for (int y : G[x]){
		if (!vis[y])
			res = max(res, dfs(y));
	}
	used[x] = 1;
	ans[x] = res;
	return res;
}
int main(){
	cin >> m >> n;
	while (n--){
		int u,v;
		cin >> u >> v;
		G[u].push_back(v);
	}
	for (int i = 1; i <= m; i++){
		if (!used[i])
			dfs(i);
		fill(vis, vis + N, 0);
	}
	for (int i = 1; i <= m; i++) cout << ans[i] << " ";
}
Editor is loading...