当前位置:   article > 正文

牛客 周赛_牛客周赛

牛客周赛

Simple Game

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/44204
推荐阅读
相关标签
  

闽ICP备14008679号