跳到正文

题解

题解:CF1980G Yasya and the Mysterious Tree

yx666约 9 分钟
原文
文章信息与版本

文章信息

发布
2024年7月22日
收录
2026年9月28日
更新
2026年11月21日
篇幅
约 9 分钟
获赞
5
题目
CF1980G

版本

  1. v1当前

CF1980G Yasya and the Mysterious Tree 题解

Description\text{Description}

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

  • ^ x :所有边权异或 xx。

  • ? u x:新建一条从 uu 开始,边权为 xx 的边,你可以选择不为 uu 的任意点 vv 作为边的另一端,最大化在环上的边的边权异或和。

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

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

Solution\text{Solution}

需要用到的知识:

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

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

定义 disidis_i 表示第 ii 点到根节点的边权异或和,fi,jf_{i,j} 表示 i→ji\to j 的边权异或和。

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

fu,v=fu,lcaxor⁡flca,v=(disuxor⁡dislca)xor⁡(dislcaxor⁡disv)=disuxor⁡disv\begin{aligned} 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}

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

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

disuxor⁡xdis_u\operatorname{xor}x 已知,那我们只需要用 Trie 树的经典例题:最长异或路径 的思想,找到最大的 ansans 即可。

需要注意,u≠vu\ne v,所以要先删去点 uu,再进行查询,最后还不要忘了加回点 uu。

但是,操作 1 十分烦人,非常恶心。

很容易地想到,对于操作 1,可以实现类似线段树的懒标记,而不是每次都傻乎乎地更新。

回头看一下我们如何实现(懒标记 kk,每个点深度 hih_i):

  1. 转化为有根树。

  2. 预处理 disidis_i。

  3. 对于每个操作 1,将 k=kxor⁡xk=k\operatorname{xor}x。

  4. 对于每个操作 2,将所有 s(v)s(v) 插入一棵 Trie 树,贪心查询,第 ii 点的 disidis_i,会异或上 hih_i 个 kk。

我们惊奇地发现,第四步还可以进一步优化:

操作 1 只会影响奇数深度的那些点,故我们可以开两个 Trie,分别维护奇数层与偶数层的点的集合。剩下的操作与上述是相同的。

具体的,奇数层每次查询与 disxxor⁡kxor⁡xdis_x\operatorname{xor}k\operatorname{xor}x 的异或最大值,偶数层每次查询与 disxxor⁡xdis_x\operatorname{xor}x 的异或最大值,而不用每次去更改 disidis_i。

时间复杂度 O((n+q)log⁡x)O((n+q)\log x)。

Code\text{Code}
C++
#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;
}
原文
存档评论4 条抓取于