题解:P14760 [Opoi 2025] CCD 的游戏

· · 题解

因为T1不会做,所以来把T2给做了。

动态规划,定义 f_u 为在 u 为根的子树中,标记点 u 的人能否赢。

显然当 u 为叶子节点时 f_u=1,然后分情况讨论:

::::info[转移]{open}

::::info[原因]

为了方便,把标 u 一方的叫做先手:

::::

由于初始时根节点已经是被标记的,所以先后手互换,所以答案也取反。

::::success[AC代码]

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e6+10;
ll t,n;
vector<ll> G[N];
ll f[N],lst;
void dfs(ll u,ll fa){
    ll x=0,h=0;
    for(auto v:G[u]){
        if(v==fa) continue;
        dfs(v,u);
        if(f[v]==1) x++;
        else h++;
    }
    if(x==0&&h==0) f[u]=1;
    else if(x==0) f[u]=1;
    else if(h==0) f[u]=0;
    else f[u]=0;
    return;
}
int main(){
    cin>>t;
    while(t--){
        for(ll i=1;i<=lst;i++) f[i]=-114514;
        for(ll i=1;i<=lst;i++) G[i].clear();
        cin>>n;
        lst=n;
        for(ll i=1;i<n;i++){
            ll x,y;
            cin>>x>>y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        dfs(1,0);
        ll ans=1-f[1];
        if(ans==1) cout<<"first\n";
        else cout<<"second\n";
    }
    return 0;
}

::::