The topic of general XOR is solved with trie.
The problem is the trie on the tree;
First: From Root==1, traverse the tree to get the XOR value of 1 to all nodes.
Then for each point we insert it into the binary tree.
For each point lookup, the number of binary values that are different or the maximum value is traversed sequentially.
Note: The number of edges is more than twice times, re many times.
The Find function is specific to this:
Binary value for a book: 10001000101
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5#include <string>6#include <cstring>7#include <Set>8#include <map>9#include <stdlib.h>Ten One #defineN 223456 A using namespacestd; - structEdge - { the intV,w,next; - }e[n]; - intTot,nid; - intHead[n],ans[n]; + intnext[n* -][2]; - voidAddintUintVintW) + { Ae[tot].v=v; ate[tot].w=W; -e[tot].next=Head[u]; -head[u]=tot++; - } - - voidDfsintUintFA) in { - for(inti=head[u];i!=-1; i=e[i].next) to { + intv=e[i].v; - if(V==FA)Continue; theans[v]=ans[u]^E[I].W; * DFS (v,u); $ }Panax Notoginseng } - the voidInsertintNodeintDintval) + { A if(d== -)return; the intp= in-D; + intC= (val& (1<<p))?1:0; - $ if(next[node][c]==-1) next[node][c]=++nid; $Insert (next[node][c],d+1, Val); - } - the intSolveintNodeintDintval) - {Wuyi if(d== -)return 0; the intp= in-D; - intC= (val& (1<<p))?0:1; Wu - if(next[node][c]!=-1) About return(1<<p) +solve (next[node][c],d+1, Val); $ - returnSolve (next[node][!c],d+1, Val); - } - A intMain () + { the intT; -scanf"%d",&T); $ while(t--) the { the intN; thescanf"%d",&n); thememset (head,-1,sizeof(head)); -memset (next,-1,sizeof(next)); inmemset (ans,0,sizeof(ans)); theTot=nid=0; the for(intI=1; i<n;i++) About { the intu,v,w; thescanf"%d%d%d",&u,&v,&W); the Add (u,v,w); + Add (v,u,w); - } the BayiDfs1,-1); the for(intI=1; i<=n;i++) Insert (0,0, Ans[i]); the inttmp=0; - - for(intI=1; i<=n;i++) theTmp=max (Tmp,solve (0,0, Ans[i])); theprintf"%d\n", TMP); the } the return 0; -}
View Code
Let's just Judge 01110111010.
There is no so that the maximum value can be reached
Codechef Polo the Penguin and the Tree