题解:CF1980G Yasya and the Mysterious Tree

· · 题解

CF1980G Yasya and the Mysterious Tree 题解

\text{Description}

一棵有边权的树,存在两种操作:

注意每个操作 2 没有实际意义上的加边。

对于 100\% 的数据,2\le n\le2\times10^5,0\le w,x\le 10^9,\sum Q\le 2\times10^5,1\le T\le 10^4,输入保证是一棵树。

\text{Solution}

需要用到的知识:

  1. 树上 bfs 或 dfs。
  2. Trie 树。

首先,将原树转换为有根树,这里不妨让根节点为 1。

定义 dis_i 表示第 i 点到根节点的边权异或和,f_{i,j} 表示 i\to j 的边权异或和。

那么对于树上的节点 u,v,它们之间的距离就为 dis_u\operatorname{xor} dis_v,证明如下(lca 是 u,v 的最近公共祖先,抓住 a\operatorname{xor}a=0,a\operatorname{xor}0=a):

f_{u,v}&=f_{u,lca}\operatorname{xor}f_{lca,v}\\ &=(dis_u\operatorname{xor}dis_{lca})\operatorname{xor} (dis_{lca}\operatorname{xor}dis_v)\\ &=dis_u\operatorname{xor}dis_v\end{aligned}

所以我们需要维护所有的 dis_i 即可。

又记每个操作 2 的答案为 ans,则 ans=dis_u\operatorname{xor}dis_v\operatorname{xor}x。

需要注意,$u\ne v$,所以要先删去点 $u$,再进行查询,最后还不要忘了加回点 $u$。 但是,操作 1 十分烦人,非常恶心。 很容易地想到,对于操作 1,可以实现类似线段树的懒标记,而不是每次都傻乎乎地更新。 回头看一下我们如何实现(懒标记 $k$,每个点深度 $h_i$): 1. 转化为有根树。 2. 预处理 $dis_i$。 3. 对于每个操作 1,将 $k=k\operatorname{xor}x$。 4. 对于每个操作 2,将所有 $s(v)$ 插入一棵 Trie 树,贪心查询,第 $i$ 点的 $dis_i$,会异或上 $h_i$ 个 $k$。 我们~~惊奇地~~发现,第四步还可以进一步优化: 操作 1 只会影响奇数深度的那些点,故我们可以开两个 Trie,分别维护奇数层与偶数层的点的集合。剩下的操作与上述是相同的。 具体的,奇数层每次查询与 $dis_x\operatorname{xor}k\operatorname{xor}x$ 的异或最大值,偶数层每次查询与 $dis_x\operatorname{xor}x$ 的异或最大值,而不用每次去更改 $dis_i$。 时间复杂度 $O((n+q)\log x)$。 $$\text{Code}$$ ``` cpp #include<bits/stdc++.h> using namespace std; #define T int static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf; #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++ #define pc(x) (p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x) #define io_flush fwrite(obuf,p3-obuf,1,stdout) static inline T read(){T x=0;char ch=gc();bool syn=0;while(ch<'0'||'9'<ch) syn|=ch=='-',ch=gc();while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+(ch-'0');ch=gc();}return syn?-x:x;} static inline void writesp(T x) {static int sta[28];if(x<0){pc('-');x=-x;}int top = 0;do sta[top++] = x % 10, x /= 10;while (x);while (top) pc(sta[--top] + '0');pc(' ');} #undef T static inline int max(int a,int b){return a>b?a:b;} #define N 200005 #define M 400005 int tot=0,head[N]; struct{ int to,nxt,val; }edge[M]; void addEdge(int from,int to,int val){ edge[++tot].to=to; edge[tot].nxt=head[from]; edge[tot].val=val; head[from]=tot; return; } // 建树 int dis[N]; bitset<N>h,vis; static inline void bfs(int rx){ // 预处理 h,dis queue<int>q; q.push(rx); h.reset(),vis.reset(); h[rx]=1; while(q.size()){ int x=q.front();q.pop(); if(vis[x]) continue; vis[x]=1; for(int i=head[x];i;i=edge[i].nxt){ int y=edge[i].to; if(!vis[y]){ h[y]=!h[x]; dis[y]=dis[x]^edge[i].val; q.push(y); } } }return ; } class Trie{ private: int cnt=0,trie[N<<5][2],tot[N<<5]={0}; public: void clear(){ // 清空 for(int i=0;i<=cnt;++i) trie[i][0]=trie[i][1]=tot[i]=0; cnt=0; }; void insert(int a,int d){ // 向 a 处插入 d int p=0; for(int i=31;i>=0;--i){ bool x=(a>>i)&1; if(!trie[p][x]) trie[p][x]=++cnt; p=trie[p][x]; tot[p]+=d; } return ; } int query(int a){ // 查询 a int p=0,ans=0; for(int i=31;i>=0;--i){ bool x=(a>>i)&1; if(trie[p][!x]&&tot[trie[p][!x]]>0){ // 注意 tot 里面是 trie[p][!x] 而不是 p p=trie[p][!x]; ans|=1<<i; }else p=trie[p][x]; } return ans; } }trie[2]; static inline void solve(){ int n=read(),q=read(); for(int i=1;i<n;++i){ int u=read(),v=read(),w=read(); addEdge(u,v,w);addEdge(v,u,w); } bfs(1); for(int i=1;i<=n;++i) trie[h[i]].insert(dis[i],1); int k=0; while(q--){ char ch=gc(); while(ch!='^'&&ch!='?') ch=gc(); if(ch=='^'){ k^=read(); }else{ int u=read(),x=read(); trie[h[u]].insert(dis[u],-1); int ans=trie[h[u]].query(dis[u]^x); trie[h[u]].insert(dis[u],1); ans=max(ans,trie[!h[u]].query(dis[u]^x^k)); writesp(ans); } }pc('\n'); // 清空 fill(head,head+1+n,0); tot=0; trie[0].clear();trie[1].clear(); return ; } signed main(){ int t=read(); while(t--) solve(); io_flush; return 0; } ```