P4551 (poj3764) Maximum exclusive or path

Source: Internet
Author: User

Description

Given a weighted tree with N points, The subscripts start from 1 to n. Find two nodes in the tree and find the longest exclusive or path.

An exclusive or path refers to the exclusive or of all edge weights in a unique path between two nodes.

Individuals:

First, I strongly recommend 01 dictionary tree (trie). This is a solution.XOR Problems.

When we look for the maximum variance or value, we generally look down from the highest bit to the low bit.

   eg:   1000(2)=8(10)         0111(2)=7(10)

Obviously, as long as my highest bit is 1, unless you and I have the same highest bit, I am bigger than you.

According to the sum of the mathematical proportional series, we can see that
8 = 2 ^ 3, 7 = 2 ^ 3-1

So we canGreedy to find the current node ^ 1

01 The dictionary tree method is similar to the trie tree method. For this question,

Process: 1. Create a graph and run DFS to find the XOR value from each node to the root node. 2. Build 01trie again to implement our greed.

I have talked a lot about this on the Internet. I want to learn Baidu.

------- Code ---------

#include<bits/stdc++.h>#define IL inline#define RI register int#define maxn 100008int trie[maxn*31][2],xo[maxn],ans,rt;int val[maxn],n,head[maxn],tot;struct code{int u,v,w;}edge[maxn<<1];IL void read(int &x){    int f=1;x=0;char s=getchar();    while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}    while(s<='9'&&s>='0'){x=x*10+s-'0';s=getchar();}    x*=f;}IL void add(int x,int y,int z){    edge[++tot].u=head[x];    edge[tot].v=y;    edge[tot].w=z;    head[x]=tot;    edge[++tot].u=head[y];    edge[tot].v=x;    edge[tot].w=z;    head[y]=tot;}IL void build_trie(int x,int rt){    for(RI i=1<<30;i;i>>=1)    {        bool c=x&i;        if(!trie[rt][c])trie[rt][c]=++tot;        rt=trie[rt][c];    }}IL int query(int x,int rt){    int ans=0;    for(RI i=1<<30;i;i>>=1)    {        bool c=x&i;        if(trie[rt][c^1])ans+=i,rt=trie[rt][c^1];        else rt=trie[rt][c];    }    return ans;}IL void dfs(int u,int fa){    for(RI i=head[u];i;i=edge[i].u)    {        if(edge[i].v!=fa)        {            xo[edge[i].v]=xo[u]^edge[i].w;            dfs(edge[i].v,u);        }    }}int main(){    read(n);    for(RI i=1,u,v,w;i<n;i++)read(u),read(v),read(w),add(u,v,w);    dfs(1,0);    for(RI i=1;i<=n;i++)build_trie(xo[i],rt);    for(RI i=1;i<=n;i++)ans=std::max(ans,query(xo[i],rt));    printf("%d",ans);}

P4551 (poj3764) Maximum exclusive or 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.