#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 9;
ll n, x, k, m;
int f[maxn];
bitset <maxn> vis;
struct node
{
int to,next;
}e[maxn << 2];
int head[maxn], cnt;
void add(int x, int y)
{
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
void dfs(int x)
{
vis[x] = 1;
for(int i = head[x]; i; i = e[i].next)
{
int tmp = e[i].to;
if(!vis[tmp]) dfs(tmp);
f[x] = min(f[tmp], f[x]);
}
}
void work()
{
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y;
cin >> x >> y;
add(x, y);
}
for(int i = 1; i <= n; ++i) f[i] = i;
for(int i = n; i >= 1; --i)
{
if(!vis[i]) dfs(i);
}
vis.reset();
for(int i = n; i >= 1; --i)
{
if(!vis[i]) dfs(i);
}
vis.reset();
sort(f + 1, f + 1 + n);
for(int i = 1; i <= n; ++i) if(f[i] != f[i-1])
cout << f[i] << " ";
}
int main()
{
ios::sync_with_stdio(0);
//int TT;cin>>TT;while(TT--)
work();
return 0;
}
牛客 周赛
最新推荐文章于 2025-03-03 20:17:09 发布
该博客介绍了一个名为SimpleGame的游戏,并展示了如何使用图的深度优先搜索(DFS)来寻找从源节点到所有其他节点的最短路径。算法首先初始化路径长度,然后进行两次DFS遍历,分别从叶子节点向上和从根节点向下,最终输出不重复的最短路径节点序列。
388

被折叠的 条评论
为什么被折叠?



