牛客練習賽63 牛牛的樹行棋

鏈接

點擊跳轉

賽時體驗

一開始讀錯了題耽誤一個小時…

最後五分鐘想出咋做了但是來不及寫完

題解

因爲我可以把一個石子放到兒子裏去

假設這個點距離其子樹中最深的葉子的距離是dd,那麼這個石子的深度就可以變成d1,d2,...,0d-1,d-2,...,0(在葉子上就是00)

那其實就是個石子堆,有dd個石子,這個和nimnim的區別就在於移動到不同的深度會有不唯一的方案,但是就表面現象來看還是石子堆遊戲

所以只要把每個點的dd異或起來就知道是否必勝,假設異或和爲nimnim

統計方案數:

假設我要移動位於ii的石子,那麼要使得移動之後變成必敗局面,設移動之後剩下xx個石子,就有nimxordixorx=0nim \mathop{xor} d_i \mathop{xor} x = 0

解得x=nimxordix=nim \mathop{xor} d_i

那就成了統計子樹裏有多少個jj滿足dj=xd_j = x,做這個事情可以直接dsu on treedsu\ 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章