poj-3764 the Xor-longest Path

Source: Internet
Author: User

Test instructions
Gives a right tree, seeking the maximum XOR of the two-point path on the tree;

n<=100000;


Exercises

Considering the nature of the XOR, if any point is chosen as the root, the different or depth of all points is processed;

Then the depth of two points or up, LCA to the root of the path is different or two times the equivalent of no;

So the XOR or distance is the xor of the two points or the depth, and the problem is transformed into two numbers from the N number to make the XOR and the maximal;

This classic problem can be stored in the number of bits into the 01trie tree, from high to low greedy solution;

Complexity of O (31n);

HINT:

It is recommended to use unsigned int;

Deep search does not seem to explode stack;

The dictionary tree opens 31 times times the space, I just because this first re in the MLE = =;

A little card vector,(chain forward to the star do not write wrong) I just because this tle changed to WA = =;


Code:


#include <vector> #include <stdio.h> #include <string.h> #include <algorithm> #define N 110000using namespace std;typedef unsigned int it;it to[n<<1],val[n<<1],p[n<<1],head[n],tot;it dis[ N],next[n<<5][2],root,cnt,ans;bool vis[n];void init () {Cnt=1,root=1,ans=0,tot=0;memset (head,0,sizeof (head)) ; memset (Next,0,sizeof (next)); Memset (vis,0,sizeof (Vis));} void Add (it x,it Y,it v) {To[++tot]=y;p[tot]=head[x];val[tot]=v;head[x]=tot;} void Dfs (it x,it pre,it d) {dis[x]=d;it i,y;for (I=head[x];i;i=p[i]) {if ((y=to[i))!=pre) DFS (Y,x,d^val[i]);}} void Insert (it x) {it t=1<<31,p=root;bool Index;while (t) {index=t&x?1:0;if (next[p][index]==0) Next[p][index ]=++cnt;p=next[p][index];t>>=1;}} void query (it x) {It t=1<<31,p=root;it ret=0;bool index;while (t) {index=t&x?0:1;if (Next[p][index]) p=next[p] [Index],ret|=t;elsep=next[p][!index];t>>=1;} Ans=max (Ans,ret);} int main () {It n,m,i,j,k,x,y,v;while (scanf ("%d", &n)!=eof) {init (); for (i=1;i<n;i++) {scanf ("%d%d%d", &x,&y,&v); X++,y++;add (x,y,v); add (y,x,v);} DFS (1,0,0), insert (0), for (i=2;i<=n;i++) {query (dis[i]), insert (Dis[i]);} printf ("%d\n", ans);} return 0;}


poj-3764 the Xor-longest Path

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.