Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 5646 |
|
Accepted: 1226 |
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)
Test instructions: Give a Benquan tree of n nodes, find a path (u,v), so that the weight of the edge on the path is different or the maximum value
Idea: We can first 0 for the root, find the other node I to the root of the path of the Benquan XOR value d[i], for u,v between the Benquan of the path or the result is d[u]^d[v], then the problem is converted to give the number of N, the maximum value of any two XOR
Each number in binary form from high to low insert trie, and then enumerate each number, in trie greedy, that is, the current is 0 to go to 1, 1 to 0 walk.
Start writing dynamically assigned nodes Trie has been tle ...
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>using namespaceStd;typedefLong Longll;Const intN =100005;intN;structEdge {intV, W, NEX; Edge () {} Edge (intVintWintNEX): V (v), W (W), NEX (NEX) {}}; Edge E[n<<1];inthead[n], tot;voidAddintUintVintW) {E[tot]=Edge (V, W, Head[u]); Head[u]= tot++;}voidRead () {memset (head,-1,sizeofhead); Tot=0; intu, V, W; for(inti =1; I < n; ++i) {scanf ("%d%d%d", &u, &v, &W); Add (U, V, W); Add (V, u, W); }}intD[n], vis[n];voidBFs () {Queue<int>que; Que.push (0); d[0] =0; memset (Vis,0,sizeofvis); intu, V, W; vis[0] =1; while(!Que.empty ()) {u=Que.front (); Que.pop (); for(inti = Head[u]; ~i; i =E[i].nex) {v= E[I].V; W =E[I].W; if(Vis[v])Continue; D[V]= D[u] ^W; VIS[V]=1; Que.push (v); } }}intCh[n * +][2];structTrie {intsz; Trie () {sz=1; memset (ch[0],0,sizeofch[0]); } void_insert (intbs[]) { intU =0; for(inti = -; I >=0; --i) {intc =Bs[i]; if(!Ch[u][c]) {memset (Ch[sz],0,sizeofCh[sz]); CH[U][C]= sz++; } u=Ch[u][c]; } } int_search (intbs[]) { intU =0, ans =0; for(inti = -; I >=0; --i) {intc =Bs[i]; if(c = =1) { if(ch[u][0]) {ans + = (1<< (i)); U = ch[u][0]; } ElseU = ch[u][1]; }Else { if(ch[u][1]) {ans + = (1<< (i)); U = ch[u][1]; } ElseU = ch[u][0]; } } returnans; }};intans;intb[ *];void Get(intx) {intls =0; memset (b,0,sizeofb); while(x) {B[ls+ +] = x%2; X>>=1; }}voidsolve () {Trie mytrie; Ans=0; for(inti =0; I < n; ++i) {Get(D[i]); Mytrie._insert (b); Ans=Max (ans, mytrie._search (b)); } printf ("%d\n", ans);}intMain () {//freopen ("In.txt", "R", stdin); while(~SCANF ("%d", &N)) {read (); BFS (); Solve (); }}
View Code
Poj the Xor-longest Path Classic Trie Any of the two different or maximum values in the N number