| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 6361 |
|
Accepted: 1378 |
Description
In an edge-weighted tree, the xor-length of a path p was defined as the XOR sum of the weights of edges on p :
⊕is the XOR operator.
We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?
Input
The input contains several test cases. The first line of all test case contains an integer n(1<=n<=100000), the following n-1 L Ines each contains three integers u(0 <= u < n),v(0 <= v < n< /c8>),W(0 <= w < 2^31), which means there is an edge between node u and v of length W.
Output
For each test case output the xor-length of the xor-longest path.
Sample Input
40 1 31 2 41 3 6
Sample Output
7
Hint
The xor-longest path is 0->1->2, which have length 7 (=3⊕4)
Source
Trie Tree + Greedy
DFS preprocessed The XOR of the path to the root of each node. The XOR of the path between two points and equal to the XOR of the path of the individual to the root.
Convert all the XOR and transform into a binary string and build the trie tree.
For each binary string, the greedy selection on the trie tree makes the path with the greatest difference or value (as far as possible to the opposite node) and records the optimal answer.
↑ in order to prevent matching to a part of itself, each binary string should be checked before inserting the trie tree.
1 /*by Silvern*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8 using namespacestd;9 Const intmxn=200010;Ten intRead () { One intx=0, f=1;CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; the } - structedge{ - intv,nxt,w; -}e[mxn<<1]; + inthd[mxn],mct=0; - voidAdd_edge (intUintVintW) { +E[++MCT].V=V;E[MCT].NXT=HD[U];E[MCT].W=W;HD[U]=MCT;return; A } at intt[mxn*Ten][2],cnt=1; - intn,ans=0; - intF[MXN]; - voidInsertintx) { - intnow=1; - for(intI= to; i>=0; i--){ in intv= (x>>i) &1; - if(!t[now][v]) t[now][v]=++CNT; tonow=T[now][v]; + } - return; the } * voidQueryintx) { $ intnow=1;Panax Notoginseng intres=0; - for(intI= to; i>=0; i--){ the intv= (x>>i) &1; + if(t[now][v^1]){ Av^=1; theres+= (1<<i); + } -now=T[now][v]; $ } $ans=Max (Res,ans); - return; - } the voidDFS (intUintFaintnum) { -f[u]=num;Wuyi for(intI=hd[u];i;i=e[i].nxt) { the intv=e[i].v; - if(V==FA)Continue; WuDFS (v,u,num^E[I].W); - } About return; $ } - voidinit () { -memset (HD,0,sizeofHD); -memset (T,0,sizeoft); A //memset (f,0,sizeof f); +mct=0; cnt=1; ans=0; the return; - } $ intMain () { the while(SCANF ("%d", &n)! =EOF) { the init (); the inti,j,u,v,w; the for(i=1; i<n;i++){ -U=read (); V=read (); w=read (); inu++;v++; the Add_edge (u,v,w); the Add_edge (v,u,w); About } theDFS (1,0,0); the for(i=1; i<=n;i++){ the //printf ("f[%d]:%d\n", I,f[i]); + query (F[i]); - Insert (F[i]); the }Bayiprintf"%d\n", ans); the } the return 0; -}
POJ3764 the Xor-longest Path