// https://www.luogu.com.cn/problem/P3916
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
int n, m;
int idx, h[N], e[N], ne[N];
bool st[N];
int ans[N];
void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
int dfs(int cur) {
if (st[cur]) return ans[cur];
st[cur] = 1;
int res = cur;
for (int i = h[cur]; i != -1; i = ne[i]) {
int j = e[i];
res = max(res, dfs(j));
}
ans[cur] = res;
return res;
}
void solve() {
cin >> n >> m;
memset(h, -1, sizeof(int) * (n + 3));
while (m--) {
int a, b;
cin >> a >> b;
add(a, b);
}
for (int i = 1; i <= n; i++) {
dfs(i);
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int t;
// cin >> t;
t = 1;
while (t--) {
solve();
}
return 0;
}
为什么记忆化搜索只有40分呢?