牛客 周赛

Simple Game

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

你可能感兴趣的:(图论,c++,linux)