You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
814 B
35 lines
814 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 1e5 + 10, M = N << 1;
|
|
|
|
int n, m;
|
|
// 链式前向星
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
void add(int a, int b, int c = 0) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
int res[N];
|
|
|
|
void dfs(int u, int val) {
|
|
if (res[u]) return; // 如果已经标记过,不用重复标记
|
|
res[u] = val; // 没有标记过,就标记一下
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (!res[j]) dfs(j, val);
|
|
}
|
|
}
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
while (m--) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
add(b, a);
|
|
}
|
|
|
|
for (int i = n; i >= 1; i--) dfs(i, i);
|
|
for (int i = 1; i <= n; i++) cout << res[i] << ' ';
|
|
return 0;
|
|
}
|