题解:AT_abc296_e [ABC296E] Transition Game

题目传送门

思路

我们可以在环中任选一点,然后在环内可以转到另一个点。因为起点自由选择,所以环中每个点都可以到达,由此我们可以得知环上的所有点都是必胜点。

我们把这个问题抽象为一张图,用拓扑排序判环即可。

AC 代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
int n,a[N],ind[N],ans;queue<int> q; 

void ts(){//拓扑排序
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (!--ind[a[u]]){
			q.push(a[u]);
		} 
	}
}

int main(){
	ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
	cin>>n;
	for (int i = 1; i <= n; i++)cin>>a[i],ind[a[i]]++;
	for (int i = 1; i <= n; i++)if (!ind[i])q.push(i);
	ts();
	for (int i = 1; i <= n; i++)ans += ind[i];
	cout<<ans;
	return 0;
}
posted @ 2024-12-17 14:13  Zheng_iii  阅读(10)  评论(0)    收藏  举报