CF 1033C Permutation Game 拓扑+排序
题意:一个全排列,alice可以从某一个数出发,从i走到j的条件是:
a[j]>a[i],而且从i到j要符合|i-j|%a[i]=0,若alice在该数有必胜的策略,输出B,否则A
思路,拓扑排序+博弈论(这题让我做的太迷了刚刚) ,用邻接表连接该数与其他数的关联,如果一开始入度为0的,即alice选择该点后,对方无法走去另一个点,alice会赢,把它们坐标存进一个数组里,再把它们连边进行分析,就可以判断出谁输谁赢,详情可看代码#include <iostream>
#include <vector> #include<cmath> #include <set> #include <map> #include<cstring> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define N 200005 #define fori for(i=0;i<n;i++) #define fori1 for(i=1;i<=n;i++) ll a[N]; vector<ll>G[N]; char s[N]; ll degree[N]; ll q[N]; ll vis[N]; void dfs() { } int main() { ll i, j, k; ll n; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for(i=1;i<=n;i++) { for (j = i + a[i]; j <= n; j += a[i]) if (a[j] > a[i])G[j].push_back(i), degree[i]++;//连边 for (j = i - a[i]; j >= 1; j -= a[i]) if (a[j] > a[i])G[j].push_back(i), degree[i]++; } memset(vis, -1, sizeof(vis)); ll L = 1, R = 0; for (i = 1; i <= n; i++) if (!degree[i]) q[++R] = i, vis[i] = 0;//出度,放入q数组,该点一定为alice赢(选择该点就无法走) while (L <= R) { int u = q[L++]; for (i = 0; i < G[u].size(); i++)//与这个点有关系的一定会alice放完后对手会放到这个点上 { int v = G[u][i]; if (vis[v] == -1) {//如果vis[u]是alice赢,则v点对手赢,否则对手输 if (vis[u] == 0)vis[v] = 1; else vis[v] = 0; } else if (vis[u] == 0)vis[v] = 1; degree[v]--;//出度 if (!degree[v]) q[++R] = v;//出度,该点也成为必胜点 } } for (i = 1; i <= n; i++) if (vis[i])cout << 'A'; else cout << 'B'; }