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

牛牛的树行棋 (树形sg函数)

在这里插入图片描述
题意如上
思路:

  1. 很好的一道树上博弈。。。首先需要看出来是sg博弈(并且知道sg博弈)
  2. 知道之后 如果能猜到sg函数的表示就差不多啦,结论是:sg[x]=到达子树中最深的叶子节点的长度。
  3. 那么怎么猜呢???叶子节点无法在向下延伸,sg=0.那么叶子节点的father只能向下延伸一步,合理的猜想过后sg=1。之后balabala就能得出 这个结论:结论是:sg[x]=到达子树中最深的叶子节点的长度。虽然我是看题解猜出来的
  4. 之后就是应有的套路,sg异或和xorsum。如果xorsum=0 先手必败。否则先手必剩。这个很简单一遍dfs即可得出来
  5. 之后考虑第二个问题,当xorsum!=0时,如何操作可以让异或和等于0.balabala。。。很明显如果两个节点的异或和为xorsum(这两个节点是属于跟与子树上的节点的关系)那么我们就可以把该根节点移动到该子树上的这个节点上。就会神奇的发现现在行异或和就是0…哈哈哈,俺就可以赢了
  6. 那么问题又来了,如何求任意两个节点的异或值为xorsum且属于跟与子树的关系呢?
  7. myway:抓住一个节点 向上找(上面的节点肯定都可能成为他的跟) 可以用dfs 进出栈的特点。维护到当前节点的这条链的值为k的数量cnt[k],每次sum+=cnt[xorsum^sg[u]]即可
  8. otherway:抓住一个节点向下找 (下面的所以节点都可能成为答案) 也是dfs 差分的思想 进入这个节点时减去cnt[xorsum^sg[u]] ,出去这个节点的时候加上 xorsum^sg[u]即可

这两种做法都是特别好的思路

myway:

void dfs2(int u,int fa){
    ans+=V[res^sg[u]];
    V[sg[u]]++;
    for(auto v:edge[u]){
        if(v==fa) continue;
        dfs2(v,u);
    }
    V[sg[u]]--;
}

dalaoway

void dfs2( int x,int f )
{
    ans-=cnt[mx[x]^res];
    cnt[mx[x]]++;
    for( auto v:g[x] )
    {
        if( v==f ) continue;
        dfs2(v,x);
    }
    ans+=cnt[mx[x]^res];
}

完整代码

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
//======================================
namespace FastIO{
char print_f[105];void read() {}void print() {putchar('\n');}
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth){x = 0;char ch = getchar();ll f = 1;while (!isdigit(ch)){if (ch == '-')f *= -1;ch = getchar();}while (isdigit(ch)){x = x * 10 + ch - 48;ch = getchar();}x *= f;read(oth...);}
template <typename T, typename... T2>
inline void print(T x, T2... oth){ll p3=-1;if(x<0) putchar('-'),x=-x;do{print_f[++p3] = x%10 + 48;}while(x/=10);while(p3>=0) putchar(print_f[p3--]);putchar(' ');print(oth...);}} // namespace FastIO
using FastIO::print;
using FastIO::read;
//======================================
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int maxn = 1e6+5;
vector<int>edge[maxn];
int sg[maxn],V[maxn];
int n,res,ans;
void add(int x,int y){
    edge[x].push_back(y);
    edge[y].push_back(x);
}
int dfs(int u,int fa){
    sg[u]=sg[fa]+1;
    int Max=sg[u];
    for(auto v:edge[u]){
        if(v==fa) continue;
        int t=dfs(v,u);
        Max=max(t,Max);
    }
     sg[u]=Max-sg[u];
    return Max;
}
void dfs2(int u,int fa){
    ans+=V[res^sg[u]];
    V[sg[u]]++;
    for(auto v:edge[u]){
        if(v==fa) continue;
        dfs2(v,u);
    }
    V[sg[u]]--;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("H:\\code\\in.in", "r", stdin);
    freopen("H:\\code\\out.out", "w", stdout);
    clock_t c1 = clock();
#endif
//**************************************
    read(n);
    for(int i=1;i<n;i++){
        int x,y;
        read(x,y);
        add(x,y);
    }
     dfs(1,0);
     for(int i=1;i<=n;i++){
         res^=sg[i];
     }
     if(res==0){
         puts("NO");
     }
     else{
         puts("YES");
         dfs2(1,0);
         print(ans);
     }
    // cout<<"\n";
//**************************************
    
#ifndef ONLINE_JUDGE
    cerr << "Time:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值