# 【2019.8.6 慈溪模拟赛 T2】树上路径（tree）（Trie）

### 从暴力考虑转化题意

$v_x\ xor\ v_y\ xor\ v_{LCA(x,y)}\ xor\ v_{LCA(x,y)}$

$v_x\ xor\ v_y$

### 代码

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 1000000
#define Gmax(x,y) (x<(y)&&(x=(y)))
using namespace std;
int n,ee,lnk[N+5];struct edge {int to,nxt,val;}e[N<<1];
class FastIO
{
private:
#define FS 100000
#define tn (x<<3)+(x<<1)
#define D isdigit(c=tc())
char c,*A,*B,FI[FS];
public:
I FastIO() {A=B=FI;}
Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
}F;
class TrieSolver
{
private:
int v[N+5];
template<int SZ,int BIT> class Trie//Trie树
{
private:
int rt,Nt,V[SZ*BIT+5],S[SZ*BIT+5][2];
I void Ins(CI v,int& rt,CI d) {!rt&&(rt=++Nt),~d&&(Ins(v,S[rt][v>>d&1],d-1),0);}//插入
I int Qry(CI v,CI rt,CI d)//求最大异或值
{
if(!rt||!~d) return 0;RI t=v>>d&1;
return S[rt][t^1]?(Qry(v,S[rt][t^1],d-1)|(1<<d)):Qry(v,S[rt][t],d-1);//能取1就取1
}
public:
I void Ins(CI v) {Ins(v,rt,BIT);}
I int Qry(CI v) {return Qry(v,rt,BIT);}
};Trie<N,30> T;
I void dfs(CI x,CI lst=0)//dfs遍历
{
RI i;for(i=lnk[x];i;i=e[i].nxt) e[i].to^lst&&
(v[e[i].to]=v[x]^e[i].val,dfs(e[i].to,x),0);T.Ins(v[x]);
}
public:
I void Solve()
{
RI i,t,ans=0;for(dfs(1),i=1;i<=n;++i) t=T.Qry(v[i]),Gmax(ans,t);//枚举v求答案
printf("%d",ans);//输出答案
}
}S;
int main()
{
freopen("tree.in","r",stdin),freopen("tree.out","w",stdout);