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.

43 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n; //n个结点
int m; //m条边
int res[N];
int u, v;
vector<int> p[N]; //vector存图
/**
dfs便
dfs
TLE
*/
/**
*
* @param x
* @param d
*/
void dfs(int x, int d) {
if (res[x]) return; //访问过
res[x] = d;
for (int i = 0; i < p[x].size(); i++) dfs(p[x][i], d);
}
int main() {
//读入
scanf("%d%d", &n, &m);
//构建图
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
p[v].push_back(u); //反向建边
}
//从大到小,逐个深度优先搜索
for (int i = n; i; i--) dfs(i, i);
//输出每个结点的最大到达结点号
for (int i = 1; i <= n; i++) printf("%d ", res[i]);
printf("\n");
return 0;
}