Namomo Spring Camp 每日一题 Week1 Day3 Dis
Problem Statement
给出 n n n 个点的一棵树, 每个点有各自的点权, 多次询问两个点简单路径所构成点集的异或和.
Solution
-
树是 n n n个点 n − 1 n-1 n−1条边的联通图.
-
树上两个点之间的路径是唯一的.
我们不妨设 1 1 1是树的根, 第 i i i个点的点权为 a i a_i ai, b i b_i bi是 1 1 1到点 i i i上点权的异或和. 我们设 P a t h ( u , v ) Path(u,v) Path(u,v)表示 u u u到 v v v路径的异或和.
P a t h ( u , v ) = P a t h ( u , l c a u , v ) x o r P a t h ( l c a u , v , v ) x o r a l c a u , v Path(u,v)=Path(u,lca_{u,v})\;xor\;Path(lca_{u,v},v)\;xor\;a_{lca_{u,v}} Path(u,v)=Path(u,lcau,v)xorPath(lcau,v,v)xoralcau,v.
指的注意的是我们要在最后再次异或 a l c a a_{lca} alca呢, 因为对于任意的数 x x o r x = 0 x\;xor\;x=0 xxorx=0. 如果我们只计算 P a t h ( u , l c a u , v ) x o r P a t h ( l c a u , v , v ) Path(u,lca_{u,v})\;xor\;Path(lca_{u,v},v) Path(u,lcau,v)xorPath(lcau,v,v)点 l c a lca lca的点权会被异或两次, 所以我们需要再次异或 a l c a u , v a_{lca_{u,v}} alcau,v.
需要对比的是, 这类问题点权和边权处理是不一样的. 如果是点权上述问题等价于 P a t h ( u , v ) = P a t h ( u , l c a ) + P a t h ( l c a , v ) − P a t h ( 1 , l c a ) − P a t h ( 1 , F a t h e r l c a ) Path(u,v)=Path(u,lca)+Path(lca,v)-Path(1,lca)-Path(1,Father_{lca}) Path(u,v)=Path(u,lca)+Path(lca,v)−Path(1,lca)−Path(1,Fatherlca).
边权的话上述问题等价于:
P a t h ( u , v ) = P a t h ( u , l c a ) + P a t h ( l c a , v ) − 2 × P a t h ( 1 , l c a ) Path(u,v)=Path(u,lca)+Path(lca,v)-2\times Path(1,lca) Path(u,v)=Path(u,lca)+Path(lca,v)−2×Path(1,lca).
这是因为我们在作前缀和的时候, 边权会记录到它的子节点上, 并不会导致计算贡献时的重复和损失.
- 其中 + / − +/- +/−为广义的运算/或者你可以理解为群论中的运算, 其中 + + +是 − - −的逆运算即可.
对于上述问题我们只需要预处理处 O ( n ) O(n) O(n)复杂度 ( D f s 即 可 ) (Dfs即可) (Dfs即可)内预处理 b i = P a t h ( 1 , i ) b_i=Path(1,i) bi=Path(1,i). 剩下每个询问只需要计算出 l c a lca lca即可.
Code(这里直接用了求树链剖分 LCA的板子)
# define Fast_IO std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
# include "unordered_map"
# include "algorithm"
# include "iostream"
# include "cstdlib"
# include "cstring"
# include "cstdio"
# include "vector"
# include "bitset"
# include "queue"
# include "cmath"
# include "map"
# include "set"
using namespace std;
namespace LCA_Tree_Chain_Subdivision{
# define maxm 500010
struct edge{
int To;
int Next;
int Value;
}Edge[maxm<<1];
int Dis[maxm],Deep[maxm],Size[maxm],Father[maxm],Son[maxm],Top[maxm];
int head[maxm],tot;
inline void Init_LCA(int N){
static int i;tot=0;
for(i=1;i<=N;i++) Dis[i]=Deep[i]=Size[i]=Father[i]=Son[i]=Top[i]=head[i]=false;
return;
}
inline void Add_Edge(int From,int To,int Value=0){
Edge[++tot]=(edge){To,head[From],Value},head[From]=tot;
return;
}
inline void Insert_Edge(int From,int To,int Value=0){
Add_Edge(From,To,Value),Add_Edge(To,From,Value);
return;
}
void Dfs1(int Now){
int i,To;
Deep[Now]=Deep[Father[Now]]+1;
Size[Now]=1;
for(i=head[Now];i;i=Edge[i].Next){
To=Edge[i].To;
if(To==Father[Now]) continue;
Father[To]=Now;
Dis[To]=Dis[Now]+Edge[i].Value;
Dfs1(To);
Size[Now]+=Size[To];
Son[Now]=Size[Son[Now]]>Size[To]?Son[Now]:To;
}
return;
}
void Dfs2(int Now,int top){
int To;
Top[Now]=top;
if(Son[Now]) Dfs2(Son[Now],top);
for(int i=head[Now];i;i=Edge[i].Next){
To=Edge[i].To;
if(To==Father[Now] || To==Son[Now]) continue;
Dfs2(To,To);
}
return;
}
inline int LCA(int x,int y){
while(Top[x]!=Top[y]){
if(Deep[Top[x]]>=Deep[Top[y]]){
x=Father[Top[x]];
}else{
y=Father[Top[y]];
}
}
return Deep[x]<Deep[y]?x:y;
}
# undef maxm
}using namespace LCA_Tree_Chain_Subdivision;
const int maxm=2e5+10;
int N,M,A[maxm],B[maxm];
vector<int> E[maxm];
void Dfs(int Now,int Father){
for(auto To:E[Now]){
if(To==Father) continue;
B[To]^=B[Now];
Dfs(To,Now);
}return;
}
int main(){
static int i,U,V;
scanf("%d%d",&N,&M);
for(i=1;i<=N;i++) scanf("%d",&A[i]),B[i]=A[i];
for(i=1;i< N;i++){
scanf("%d%d",&U,&V);
E[U].push_back(V);
E[V].push_back(U);
Insert_Edge(U,V);
}Dfs(1,1);Dfs1(1),Dfs2(1,1);
for(i=1;i<=M;i++){
scanf("%d%d",&U,&V);
printf("%d\n",A[LCA(U,V)]^B[U]^B[V]);
}
return 0;
}
Link
Link 1: Daimayuan Problem 451