跳到正文

题解

题解:AT_agc010_f [AGC010F] Tree Game

xieheng24约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月9日
收录
2026年10月12日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1

版本

  1. v1当前

思路

首先去考虑一个菊花,显然孩子权值最小值只要小于根那从根出发就是必胜的,因为其实只能从孩子和根之间跳。

推广到树上,想到每个点的所有子树其实可以被视为一个子问题,如果这个子问题是先手必败的,那肯定会往会跳,如果先手必胜游戏结束了。

那这个又变成了在先手必败点和根之间来回跳了,和菊花的情况一样了。

所以考虑枚举根,然后递归时记录一个 sti\mathrm{st}_i 表示该点及其子树从该点开始走是否后手必胜。

代码

ac 记录

C++
#include <bits/stdc++.h>
using namespace std;

namespace xieheng24 {
    #define per(i, a, b) for (int i = (a); i >= (b); --i)
    #define rep(i, a, b) for (int i = (a); i <= (b); ++i)
    #define _rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define lowbit(x) ((x) & -(x))
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    typedef pair<double, double> pdd;
    typedef pair<ll, pair<int, int>> piii;
    typedef pair<ll, int> pli;
    typedef __int128 i128;
    #define pbds __gnu_pbds
    #define pb push_back
    #define dbg(x, y, z) cout << x << " " << y << ": " << z << endl
    #ifndef dtkd
    #define ls (u << 1)
    #define rs (u << 1 | 1)
    #else
    #define ls tr[u].l
    #define rs tr[u].r
    #endif
    #ifdef fenkuai
    #define get(i) (((i) - 1) / len + 1)
    #endif
    #define _lg(x) (31 - __builtin_clz(x))
    #define _lgl(x) (63 - __builtin_clzll(x))
    #define inline_ inline __attribute__((always_inline))

    template<typename T>
    inline_ void chkmin(T& x, T y) {
        if (x > y) x = y;
    }

    template<typename T>
    inline_ void chkmax(T& x, T y) {
        if (x < y) x = y;
    }

    template<typename T1, typename T2>
    inline_ void plus(T1& x, T2 y, int mod) {
        x = (((ll)x + (ll)y) % mod + mod) % mod;
    }

    #ifdef __linux
    #define putchar putchar_unlocked
    #define getchar getchar_unlocked
    #else
    #define putchar _putchar_nolock
    #endif
    #define endl '\n'
    #define fi first
    #define se second

    constexpr int N = 3e3 + 10, M = 2 * N;
    int n;
    int a[N];
    int h[N], e[M], ne[M], idx;
    bool st[N];

    inline_ void add(int a, int b) {e[idx] = b; ne[idx] = h[a]; h[a] = idx++;}

    void dfs(int u, int p) {
        st[u] = 1;
        for (int i = h[u]; ~i; i = ne[i]) {
            int v = e[i];
            if (v == p) continue;
            dfs(v, u);
            if (a[v] < a[u] && st[v]) st[u] = 0;
        }
    }

    int main() {
        memset(h, -1, sizeof h);
        scanf("%d", &n);
        rep(i, 1, n) scanf("%d", a + i);
        rep(i, 1, n - 1) {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        rep(i, 1, n) {
            memset(st, 0, sizeof st);
            dfs(i, 0);
            if (!st[i]) printf("%d ", i);
        }

        return 0;
    }
}

signed main() {
    // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    #ifdef xh
    freopen("t.in", "r", stdin);
    freopen("t.out", "w", stdout);
    #endif

    return xieheng24::main();
}  
原文