The XOR-longest path

Source: Internet
Author: User
The XOR-longest path
Time limit:2000 ms   Memory limit:65536 K
Total submissions:3875   Accepted:850

Description

In an edge-weighted tree, the XOR-length of a pathPIs defined as the XOR sum of the weights of edges onP:

Operation 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 each test case contains an integerN(1 <=N<= 100000), the followingN-1 lines each contains three IntegersU(0 <=U<N),V(0 <=V<N),W(0 <=W<2 ^ 31), which means there is an edge between nodeUAndVOf LengthW.

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 has length 7 (= 3 limit 4)

This problem is caused by the absence of multi-group data. Wa has been around for a long time. It is relatively simple, but a xor B = a xor c xor B XOR C is used, this should be a common solution or topic skill.

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<ctime>#include<cmath>#include<algorithm>#include<set>#include<map>#include<vector>#include<string>#include<queue>#include<stack>using namespace std;#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#define MAXN 110000#define MAXV MAXN#define MAXE MAXV*2#define MAXT MAXN*200//ACtypedef long long qword;int n,m;struct trie{        int ch[2];}tree[MAXT];int toptr=1;inline void new_node(int &now){        now=++toptr;        tree[now].ch[0]=tree[now].ch[1]=0;}int dbg=0;void build_trie(qword x){        dbg++;        int now=1;        int i;        for (i=32;i>=0;i--)        {                if (!tree[now].ch[(x&(1ll<<i))!=0])                {                        new_node(tree[now].ch[(x&(1ll<<i))!=0]);                }                now=tree[now].ch[(x&(1ll<<i))!=0];        }        if (dbg==175672)                dbg--;}struct Edge{        int np;        qword val;        Edge *next;}E[MAXE],*V[MAXV];int tope=-1;void addedge(int x,int y,qword z){        E[++tope].np=y;        E[tope].val=z;        E[tope].next=V[x];        V[x]=&E[tope];}int fa[MAXN],depth[MAXN],val[MAXN];void dfs(int now,int v){        Edge *ne;        build_trie(v);        val[now]=v;        for (ne=V[now];ne;ne=ne->next)        {                if (ne->np==fa[now])continue;                fa[ne->np]=now;                dfs(ne->np,v^ne->val);        }}qword find(qword x){        int now=1;        int ret=0;        int i;        for (i=32;i>=0;i--)        {                if (tree[now].ch[(x&(1ll<<i))==0])                {                        now=tree[now].ch[(x&(1ll<<i))==0];                        ret+=1ll<<i;                }else                {                        now=tree[now].ch[(x&(1ll<<i))!=0];                }        }        if (!now)throw 1;        return ret;}int main(){        freopen("input.txt","r",stdin);        //freopen("output.txt","w",stdout);        int i,j,k;        qword x,y,z;        while (~scanf("%d",&n))        {                memset(V,0,sizeof(V));                memset(fa,-1,sizeof(fa));                toptr=1;                tope=-1;                tree[1].ch[0]=tree[1].ch[1]=0;                for (i=1;i<n;i++)                {                        scanf(LL LL LL,&x,&y,&z);                        addedge(x,y,z);                        addedge(y,x,z);                }                fa[0]=0;                dfs(0,0);                qword ans=0;                for (i=0;i<n;i++)                {                        ans=max(ans,find(val[i]));                }                printf(LL "\n",ans);        }        return 0;}

 

 

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.