Since the weights are on the edge, it is easy to find a property: D (x, y) =d (x,root) XOR D (y,root).
Because of this nature, it's good to do it. For each point count to root the distance, recorded as an F array.
Insert each value in the F array into the dictionary tree according to bits.
Enumerate each point and then search the dictionary tree for the maximum XOR value.
Program CODEVS1187;Constmaxn=100008;typeArr=RecordU,v,w,next:int64; End;typearr1=RecordNext:Array[0..1] ofLongint; End;varegArray[0.. maxn*2] ofarr; Last:Array[0.. MAXN] ofLongint; FA:Array[0.. MAXN] ofLongint; F:Array[0.. MAXN] ofInt64; T:Array[0.. maxn* +] ofarr1; A:Array[0.. -] ofLongint; N,u,v,w,root,mx,num,m,k,sum,ans,now:int64; I,j:longint;procedureAdd (u,v,w:longint);beginInc (J); EG[J].U:=u; EG[J].V:=v; EG[J].W:=W; Eg[j].next:=Last[u]; Last[u]:=J;End;procedureDFS (u:longint;sum:int64;fa:longint);varI:longint;beginF[u]:=sum; I:=Last[u]; whileI<>0 Do begin ifEg[i].v<>fa ThenDFS (Eg[i].v,sum xor eg[i].w,u); I:=Eg[i].next; End;End;beginREADLN (n); fori:=1 toN-1 Do beginreadln (U,V,W); Add (U,V,W); Add (V,U,W); End; Root:=1; DFS (Root,0,0);{---------------------------------------------------}MX:=0; fori:=1 toN Do ifF[i]>mx Thenmx:=F[i]; J:=MX; num:=0; whileJ>0 Do beginInc. (NUM); J:=jDiv 2; End; M:=num; k:=1; fori:=1 toN Do beginFillchar (A,sizeof (a),0); J:=f[i]; num:=0; whileJ>0 Do beginInc. (NUM); A[num]:=jMoD 2; J:=jDiv 2; End; Now:=1; forJ:=mDownto 1 Do ifT[now].next[a[j]]<>0 ThenNOW:=T[NOW].NEXT[A[J]]Else beginInc (k); T[NOW].NEXT[A[J]]:=K; Now:=K; End; End; Ans:=0; fori:=1 toN Do beginFillchar (A,sizeof (a),0); J:=f[i]; num:=0; whileJ>0 Do beginInc. (NUM); A[num]:=jMoD 2; J:=jDiv 2; End; Now:=1; sum:=0; forJ:=mDownto 1 Do ift[now].next[1-a[j]]<>0 Then beginsum:=sum*2+1-A[j]; Now:=t[now].next[1-A[j]]; End Else beginsum:=sum*2+A[j]; Now:=T[now].next[a[j]]; End; ifSum xor F[i]>ans Thenans:=sum xor f[i]; End; Writeln (ans);End.
CODEVS1187 xor Maximum path (dictionary tree)