ACM Learning Journey-poj 3764 the Xor-longest Path (xor && dictionary tree && Greedy)

Source: Internet
Author: User

Title Link:http://poj.org/problem?id=3764

The main idea is to seek a path in the tree, making xor and maximum.

Because it is on a tree, there should be a unique path between the two nodes.

and xor (U, v) = XOR (0, U) ^xor (0, v).

So if you preprocess the XOR path of the 0 node to all nodes , the problem is converted to the number of n to take out two numbers, making the XOR Maximum.

This has previously dealt with a similar problem with a dictionary tree.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;Const intMAXN =100005;//the maximum value of the two-energy XOR is taken out of the n number//Trie Tree Dictionary//Len*n, len number of binary maximum lengthsConst intLen = to;//Len indicates the binary maximum length of the numberstructtrie{intnext[2];} TREE[MAXN*Len];intCnttree;voidInittree () {Cnttree=0; memset (Tree,-1,sizeof(tree));voidAddintx) {    intnow =0; BOOLK;  for(inti = len; I >=0; i--) {k= x& (1<<i); if(Tree[now].next[k] = =-1) Tree[now].next[k]= ++Cnttree; now=Tree[now].next[k]; }}//returns the number of the maximum number of possible and X compositions in the current numberintQueryintx) {    intv =0, now =0; BOOLK;  for(inti = len; I >=0; i--) {k= x& (1<<i); if(Tree[now].next[!k]! =-1) K= !K; V= v| (k<<i); now=Tree[now].next[k]; }    returnv;}//chain-forward starstructedge{intto, next; intVal;} EDGE[MAXN*2];intHEAD[MAXN], CNT;voidAddedge (intUintVintW) {edge[cnt].to=v; Edge[cnt].next=Head[u]; Edge[cnt].val=W; Head[u]=CNT; CNT++;}voidInitedge () {memset (head,-1,sizeof(head)); CNT=0;}intN, P[MAXN];voidDfsintNow ) {    intto ;  for(inti = Head[now]; I! =-1; i =Edge[i].next) { to=edge[i].to; if(P[to] = =-1) {P[to]= p[now]^Edge[i].val;        DFS (to); }    }}voidinput () {Initedge (); Memset (P,-1,sizeof(p)); intu, V, W;  for(inti =1; I < n; ++i) {scanf ("%d%d%d", &u, &v, &W);        Addedge (U, V, W);    Addedge (V, U, W); } p[0] =0; DFS (0);}voidWork () {intAns =0;    Inittree ();  for(inti =0; I < n; ++i) {Add (P[i]); Ans= Max (ans, p[i]^query (P[i])); } printf ("%d\n", ans);}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%d", &n)! =EOF)        {input ();    Work (); }    return 0;}
View Code

ACM Learning Journey-poj 3764 the Xor-longest Path (xor && dictionary tree && Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.