Files
python/TangDou/LuoGuBook/P3916.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}