牛客练习赛63 牛牛的树行棋

链接

点击跳转

赛时体验

一开始读错了题耽误一个小时…

最后五分钟想出咋做了但是来不及写完

题解

因为我可以把一个石子放到儿子里去

假设这个点距离其子树中最深的叶子的距离是 d d d,那么这个石子的深度就可以变成 d − 1 , d − 2 , . . . , 0 d-1,d-2,...,0 d1,d2,...,0(在叶子上就是 0 0 0)

那其实就是个石子堆,有 d d d个石子,这个和 n i m nim nim的区别就在于移动到不同的深度会有不唯一的方案,但是就表面现象来看还是石子堆游戏

所以只要把每个点的 d d d异或起来就知道是否必胜,假设异或和为 n i m nim nim

统计方案数:

假设我要移动位于 i i i的石子,那么要使得移动之后变成必败局面,设移动之后剩下 x x x个石子,就有 n i m x o r d i x o r x = 0 nim \mathop{xor} d_i \mathop{xor} x = 0 nimxordixorx=0

解得 x = n i m x o r d i x=nim \mathop{xor} d_i x=nimxordi

那就成了统计子树里有多少个 j j j满足 d j = x d_j = x dj=x,做这个事情可以直接 d s u   o n   t r e e dsu\ on\ tree dsu on tree

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(_,__) for(auto p=__.head[_];p;p=__.next[p])
}G;
struct Easy_Tree
{
    int depth[maxn], dist[maxn], tid[maxn], rtid[maxn], tim, size[maxn], rev[maxn];
    void dfs(int pos, int pre, Graph& G)
    {
        tid[pos]=++tim;
        rev[tid[pos]]=pos;
        size[pos]=1;
        forp(pos,G)if(G.to[p]!=pre)
        {
            depth[G.to[p]]=depth[pos]+1;
            dist[G.to[p]]=dist[pos]+G.w[p];
            dfs(G.to[p],pos,G);
            size[pos]+=size[G.to[p]];
        }
        rtid[pos]=tim;
    }
    void run(Graph& G, int root)
    {
        tim=0;
        depth[root]=1;
        dfs(1,0,G);
    }
}et;
struct Longest_Chain_Decomposition
{
    ll len[maxn], son[maxn], depth[maxn], istop[maxn];
    void dfs(Graph& G, ll u, ll fa)
    {
        son[u]=0;
        len[u]=1;
        depth[u]=depth[fa]+1;
        istop[u]=false;
        forp(u,G)
        {
            ll v(G.to[p]); if(v==fa)continue;
            dfs(G,v,u);
            if(len[v]+1>len[u])len[u]=len[v]+1, son[u]=v;
        }
        forp(u,G)
        {
            ll v(G.to[p]); if(v==fa)continue;
            if(v!=son[u])istop[v]=true;
        }
    }
    void run(Graph& G, int root)
    {
        depth[0]=0, dfs(G,root,0);
        istop[root]=true;
    }
}lcd;
struct Heavy_Light_Decomposition
{
    int size[maxn], top[maxn], tid[maxn], tim, untid[maxn], depth[maxn], son[maxn], fa[maxn];
    void dfs1(Graph &G, int pos)
    {
        int p, v;
        size[pos]=1;
        for(p=G.head[pos];p;p=G.next[p])
        {
            if((v=G.to[p])==fa[pos])continue;
            fa[v]=pos;
            depth[v]=depth[pos]+1;
            dfs1(G,v);
            if(size[v]>size[son[pos]])son[pos]=v;
            size[pos]+=size[v];
        }
    }
    void dfs2(Graph &G, int pos, int tp)
    {
        int p, v;
        top[pos]=tp;
        tid[pos]=++tim;
        untid[tid[pos]]=pos;
        if(son[pos])dfs2(G,son[pos],tp);
        for(p=G.head[pos];p;p=G.next[p])
            if((v=G.to[p])!=fa[pos] and v!=son[pos])dfs2(G,v,v);
    }
    void run(Graph &G, int root)
    {
        tim=0;
        depth[root]=1;
        dfs1(G,root);
        dfs2(G,root,root);
    }
}SP;
ll want[maxn], ans, n;
ll forb, cnt[maxn], H;
void force(int pos, int pre, int opt)
{
    cnt[lcd.len[pos]-1]+=opt;
    for(auto p=G.head[pos];p;p=G.next[p])
        if(G.to[p]!=pre and G.to[p]!=forb)force(G.to[p],pos,opt);
}
void dfs(int pos, int pre, int H)
{
    for(auto p=G.head[pos];p;p=G.next[p])
        if(G.to[p]!=pre and G.to[p]!=SP.son[pos])
            dfs(G.to[p],pos,0);
    if(SP.son[pos])dfs(SP.son[pos],pos,1);
    forb=SP.son[pos];
    force(pos,pre,+1);
    ans += cnt[ want[pos] ];
    forb=0;
    if(H==0)force(pos,pre,-1);
}
int main()
{
    ll n=read(), i, u, v, nim=0, mx=-1;
    rep(i,1,n-1)
    {
        u=read(), v=read();
        G.adde(u,v), G.adde(v,u);
    }
    et.run(G,1);
    lcd.run(G,1);
    SP.run(G,1);
    rep(i,1,n)nim^=lcd.len[i]-1;
    if(nim==0)
    {
        printf("NO");
        return 0;
    }
    printf("YES\n");
    rep(i,1,n)want[i] = (nim^(lcd.len[i]-1));
    dfs(1,0,0);
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值