神奇UB,在线求调
  • 板块学术版
  • 楼主yywlp
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/12/1 17:51
  • 上次更新2023/12/1 20:26:35
查看原帖
神奇UB,在线求调
422328
yywlp楼主2023/12/1 17:51

代码第81行的W.set(a[lca])

如果加上就过了,不加就RE了

有没有大佬能告诉萌新这是为什么QAQ

#include<bits/stdc++.h>
using namespace std;
const int M=4e4+10,SZ=45;
vector<int>G[M];
void add(int u,int v){
	G[u].push_back(v);
}
int n,m;
int a[M],b[M];
bitset<M>B[SZ][SZ],W;
int dep[M],fa[M],son[M],sz[M],top[M],tot=0;
int mxd[M],id[M],tt=0;
void dfs1(int u,int f){
	sz[u]=1,fa[u]=f,dep[u]=mxd[u]=dep[f]+1;
	for(auto v:G[u]){
		if(v==f)continue;
		dfs1(v,u);
		mxd[u]=max(mxd[u],mxd[v]);
		sz[u]+=sz[v];
		if(sz[son[u]]<sz[v])son[u]=v;
	}
	if(mxd[u]-dep[u]>=1000)id[u]=++tt,mxd[u]=dep[u];
}
void dfs2(int u,int t){
	top[u]=t;
	if(son[u])dfs2(son[u],t);
	for(auto v:G[u]){
		if(v==fa[u]||v==son[u])continue;
		dfs2(v,v);
	}
}
int LCA(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	return x;
}
int from[M];
int st[M],tp=0;
void dfs2(int u){
//	cout<<endl;
//	cout<<u<<endl;
	for(auto v:G[u]){
		if(v==fa[u])continue;
		if(id[v]){
			int lst=id[st[tp]],nex=id[v];
			for(int x=v;x!=st[tp];x=fa[x])B[lst][nex].set(a[x]);//,cout<<x<<endl;
			for(int i=1;i<tp;i++)B[id[st[i]]][nex]=B[id[st[i]]][lst],B[id[st[i]]][nex]|=B[lst][nex];
			from[v]=st[tp];
			st[++tp]=v;
		}
		dfs2(v);
		if(id[v])tp--;
	}
}
int query(int x,int y){
	int lca=LCA(x,y);
	W.reset();
	while(!id[x]&&x!=lca)W.set(a[x]),x=fa[x];
	while(!id[y]&&y!=lca)W.set(a[y]),y=fa[y];
	if(x!=lca){
		int nx=x;
		while(dep[from[nx]]>dep[lca])nx=from[nx];
		if(nx==x)while(x!=lca)W.set(a[x]),x=fa[x];
		else {
			W|=B[id[nx]][id[x]];
			while(nx!=lca)W.set(a[nx]),nx=fa[nx];
		}
	}
	if(y!=lca){
		int ny=y;
		while(dep[from[ny]]>dep[lca])ny=from[ny];
		if(ny==y)while(y!=lca)W.set(a[y]),y=fa[y];
		else{
			W|=B[id[ny]][id[y]];
			while(ny!=lca)W.set(a[ny]),ny=fa[ny];
		}
	}
	W.set(a[lca]);
//	cout<<a[lca]<<endl;
//	cout<<W.count()<<endl;
	return W.count();
}
int main(){
//	freopen("std.in","r",stdin);
//	freopen("ceshi.out","w",stdout);
//	cout<<"dfs_first\n";
	cin>>n>>m;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
	sort(b+1,b+n+1);
	int len=unique(b+1,b+n+1)-b-1;
//	cout<<"A:\n";
	int maxn=0;
	for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+len+1,a[i])-b,maxn=max(maxn,a[i]);//,cout<<a[i]<<' ';
//	cout<<endl;
//	cout<<"maxn:"<<maxn<<endl;
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v),add(v,u);
	}
	dfs1(1,0),dfs2(1,1);
	if(!id[1])id[1]=++tt;
	st[++tp]=1;
	dfs2(1);
	
	int ans=0;
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		u^=ans;
		ans=query(u,v);
		printf("%d\n",ans);
	}
	return 0;
}
2023/12/1 17:51
加载中...