In an edge-weighted tree, the xor-length of a path p is 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 each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),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

4
0 1 3
1 2 4
1 3 6

Sample Output

7

Hint

The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

题意:

给出一棵树,求树上的最大路径异或和。

思路:

dfs得到根到节点的异或前缀和,然后把每个点的异或前缀和插入字典树中,就可以按套路,在字典树里找对应的最大异或了。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxm=;
const int maxn=;
int Laxt[maxm],Next[maxm],To[maxm],val[maxm],cnt,Xor[maxm];//dfs
int ch[maxn][],tot,ans,b[],n;//trie
int q_pow(int a,int x){ int res=;while(x){if(x&) res*=a;x>>=;a*=a;} return res;}
int read()
{
int res=; char c=getchar();
for(;c>''||c<'';c=getchar());
for(;c<=''&&c>='';res=res*+c-'',c=getchar()) ;
return res;
}
void init()
{
memset(Laxt,,sizeof(Laxt));
memset(Xor,,sizeof(Xor));
memset(ch,,sizeof(ch));
cnt=tot=ans=;
}
void add(int u,int v,int x)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
val[cnt]=x;
}
void dfs(int u,int pre,int x)
{
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=pre){
Xor[To[i]]=x^val[i];
dfs(To[i],u,Xor[To[i]]);
}
}
}
void insert(int x)
{
int Now=;
for(int i=;i<=;i++) { b[i]=x&;x>>=;}
for(int i=;i>=;i--){
if(!ch[Now][b[i]]) ch[Now][b[i]]=++tot;
Now=ch[Now][b[i]];
}
}
void find(int x)
{
int Now=,tmp=;
for(int i=;i<=;i++){ b[i]=x&; x>>=; }
for(int i=;i>=;i--){
if(ch[Now][b[i]^]) Now=ch[Now][b[i]^],tmp+=q_pow(,i);
else Now=ch[Now][b[i]];
} ans=max(ans,tmp);
}
void build()
{
for(int i=;i<=n;i++) insert(Xor[i]);
for(int i=;i<=n;i++) find(Xor[i]);
}
int main()
{
while(~scanf("%d",&n)){
init(); int u,v,x;
for(int i=;i<n;i++){
u=read();v=read();x=read();
u++;v++;
add(u,v,x); add(v,u,x);
}
dfs(,,); build();
printf("%d\n",ans);
} return ;
}

POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)的更多相关文章

  1. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

  2. AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  3. POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】

    题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K ...

  4. HDU4825 Xor Sum(字典树解决最大异或问题)

    Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整 ...

  5. P4551 最长异或路径 (01字典树,异或前缀和)

    题目描述 给定一棵 n 个点的带权树,结点下标从 1 开始到 N .寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 输入输出格式 输入格式: 第一行一 ...

  6. AcWing 143. 最大异或对 01字典树打卡

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  7. poj3764字典树路径最大异或和

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6853   Accepted: 1 ...

  8. poj3764 The XOR Longest Path【dfs】【Trie树】

    The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted:  ...

  9. hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。

    /** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...

随机推荐

  1. DEV GridControl 常用属性 z

    1隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[grid ...

  2. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  3. 查找——图文翔解SkipList(跳跃表)

    跳跃表 跳跃列表(也称跳表)是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作须要O(logn)平均时间). 基本上.跳跃列表是对有序的链表添加上附加的前进链接,添加是以随 ...

  4. odoo 的时差 坑 [updated]

    很多人掉进了odoo的时间坑 odoo约定关于日期的数据,存放在数据库时,以 utc0 时区也就是不带时区存放,应用程序读取日期展示日期时,转换成用户的时区展示.用户的时区通过context传递. o ...

  5. Net is as typeof 运行运算符详解 net 自定义泛型那点事

    Net is as typeof 运行运算符详解   概述 在了解运行运算符的前提我们需要了解什么是RTTI ,在任何一门面向对象的语言中,都有RTTI这个概念(即 运行时). RTTI(Run-Ti ...

  6. CentOS 6.4安装Puppet

    CentOS安装Puppet   环境介绍:centos6.4x64 採用CentOS-6.4-x86_64-minimal.iso最小化安装   puppet版本号3.6.2.ruby1.8.7,f ...

  7. 算法排序-lowB三人组

    冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇

  8. SqlServer,Oracle,Mysql 获取指定行数

    --sqlserver * FROM dbo.T_TASK --oracle --mysql ,

  9. multimap容器和multiset容器中的find操作

    前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...

  10. 2015最新iherb海淘攻略-图文入门教程-6月免邮

    注:仅仅有首次下单才享有新人优惠10$,大家下单之后千万不要取消后.否则之后则不享有新人优惠. 注:眼下Sino-海淘客国际物流已取消,仅有UCS合众速递. IHerb是美国最热门的海淘海购网站之中的 ...