"The classic application of Trie"-by Hzwer
We write down the XOR value of each point to the root and find out the maximum of two XOR values (because (a ^ c) ^ (b ^ c) = a ^ b)
So use Trie to write down each number of bits, each query when using greed, trying to go to another son can.
1 /**************************************************************2 problem:19543 User:rausen4 language:c++5 result:accepted6 time:360 Ms7 memory:27368 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <algorithm> A - using namespacestd; - Const intN =100005; the - structEdge { - intNext, to, V; - Edge () {} +Edgeint_n,int_t,int_v): Next (_n), to (_t), V (_v) {} -} e[n <<1]; + A intfirst[n], tot; at - structTrie_node { - intson[2]; -} t[3000005]; - - intCnt_trie =1, root =1; in - intN, ans; to intV[n], a[ +]; + -InlineintRead () { the intx =0, SGN =1; * CharCH =GetChar (); $ while(Ch <'0'||'9'<ch) {Panax Notoginseng if(ch = ='-') SGN =-1; -CH =GetChar (); the } + while('0'<= CH && Ch <='9') { Ax = x *Ten+ CH-'0'; theCH =GetChar (); + } - returnSGN *x; $ } $ - voidAdd_edges (intXintYintz) { -E[++tot] = Edge (first[x], y, z), first[x] =tot; theE[++tot] = Edge (First[y], x, z), first[y] =tot; - }Wuyi the voidDfsintPintFA) { - intx, y; Wu for(x = first[p]; x; x =e[x].next) - if((y = e[x].to)! =FA) { AboutV[y] = v[p] ^e[x].v; $ dfs (y, p); - } - } - A voidInsertintx) { + intNow, I, cnt_a =0; the for(i =0; I <= -; ++i) -A[i] = x &1, x >>=1; $Reverse (A, A + to); thenow =Root; the for(i =0; I <= -; ++i) { the if(!t[now].son[a[i]]) t[now].son[a[i]] = + +Cnt_trie; thenow =T[now].son[a[i]]; - } in } the the intQueryintx) { About intNow, I, cnt_a =0, res =0; the for(i =0; I <= -; ++i) theA[i] = x &1, x >>=1; theReverse (A, A + to); +now =Root; - for(i =0; I <= -; ++i) { the if(T[now].son[!a[i]]) now = T[now].son[!a[i]], res + = (1<< --i);Bayi Elsenow =T[now].son[a[i]]; the } the returnRes; - } - the intMain () { the intI, x, Y, Z; then =read (); the for(i =1; I < n; ++i) { -x = Read (), y = read (), z =read (); the add_edges (x, y, z); the } theDfs1,0);94 for(i =1; I <= N; ++i) the Insert (V[i]); the for(i =1; I <= N; ++i) theAns =max (ans, query (v[i]));98printf"%d\n", ans); About return 0; -}View Code
BZOJ1954 Pku3764 the Xor-longest Path