题解 AT2307 【[AGC010F] Tree Game】

· · 题解

[AGC010F]做题记录

对于这种博弈题, 乍一看上去没啥想法, 我们不妨由易到难, 从简单的情况入手:

如果当前只有两个节点

假设棋子在1号点, 那么先手只能往2节点走, 而后手只能返回1节点, 也就是说, 双方的选择是唯一的, 那么此时胜负情况就很明了了, 先手必胜当且

我们现在把情况变得复杂一点, 如果是这样一种情况 ![](https://cdn.luogu.com.cn/upload/image_hosting/zq7j2yze.png) 假设当前棋子在1号点, 那么先手必须向某个叶节点移动, 后手必须从叶节点反回1号点, 比较显然,先手每次移到$A_i$最小的那个叶节点是最优的, 那么此时先手必胜当且仅当$minA_i < A_1$. 我们让情况再复杂一点 ![](https://cdn.luogu.com.cn/upload/image_hosting/xlbr57jh.png) 如果棋子在6号节点, 那么先手下一步会移动到1, 或者是7。 假设在1的这棵子树内, 1号点是先手必胜的话, 那么6一定不会往1走, 因为一旦走到1, 后手就会有必胜的策略, 而如果1是后手必胜的话, 那么6往1走后, 后手一定会返回6节点, 因为继续往下走的话, 先手会有必胜策略。 这似乎又回到了我们上一幅图的情况, 先手会往一个节点走, 而该点会返回。 那么此时先手必胜, 当且仅当所有后手必胜的儿子节点A的最小值小于$A_i

于是我们可以枚举所有点作为根, dfs处理每个点儿子的情况, 然后判断该点的胜负状况, 代码十分简单。

#include<bits/stdc++.h>
#define N 10001
#define mod 1000000007

#define reg register
using namespace std;
inline int  read() {
    reg int s = 0, t = 0; reg char ch = getchar();
    while(ch > '9' || ch < '0') t |= ch == '-', ch = getchar();
    while(ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return t ? -s : s;
}
int nt[N], fi[N], go[N], dp[N], a[N], cnt, n;
int inc(int x, int y){
    return x + y >= mod ? x + y - mod : x + y;
}
void add(int x, int y){
    nt[++cnt] = fi[x]; fi[x] = cnt; go[cnt] = y;
}
void DFS(int x, int fa){
    dp[x] = false;
    for(int i = fi[x]; i; i = nt[i]){
        int v = go[i]; if(v == fa) continue;
        DFS(v, x); 
        if(dp[v] == false && a[x] > a[v]) dp[x] = true;
    }
}
int main(){
    n = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 1; i < n; i++){
        int x = read(), y =  read();
        add(x, y); add(y, x);
    }
    for(int i = 1; i <= n; i++){
        memset(dp, 0, sizeof dp);
        DFS(i, 0); if(dp[i]) printf("%d ", i);
    }
}