CF1980G Yasya and the Mysterious Tree 题解
一棵有边权的树,存在两种操作:
-
^ x:所有边权异或 。 -
? u x:新建一条从 开始,边权为 的边,你可以选择不为 的任意点 作为边的另一端,最大化在环上的边的边权异或和。
注意每个操作 2 没有实际意义上的加边。
对于 的数据,,输入保证是一棵树。
需要用到的知识:
- 树上 bfs 或 dfs。
- Trie 树。
首先,将原树转换为有根树,这里不妨让根节点为 。
定义 表示第 点到根节点的边权异或和, 表示 的边权异或和。
那么对于树上的节点 ,它们之间的距离就为 ,证明如下( 是 的最近公共祖先,抓住 ):
所以我们需要维护所有的 即可。
又记每个操作 2 的答案为 ,则 。
已知,那我们只需要用 Trie 树的经典例题:最长异或路径 的思想,找到最大的 即可。
需要注意,,所以要先删去点 ,再进行查询,最后还不要忘了加回点 。
但是,操作 1 十分烦人,非常恶心。
很容易地想到,对于操作 1,可以实现类似线段树的懒标记,而不是每次都傻乎乎地更新。
回头看一下我们如何实现(懒标记 ,每个点深度 ):
-
转化为有根树。
-
预处理 。
-
对于每个操作 1,将 。
-
对于每个操作 2,将所有 插入一棵 Trie 树,贪心查询,第 点的 ,会异或上 个 。
我们惊奇地发现,第四步还可以进一步优化:
操作 1 只会影响奇数深度的那些点,故我们可以开两个 Trie,分别维护奇数层与偶数层的点的集合。剩下的操作与上述是相同的。
具体的,奇数层每次查询与 的异或最大值,偶数层每次查询与 的异或最大值,而不用每次去更改 。
时间复杂度 。
#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;
}