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. Unity -- 入门教程三

    进入这个页面,按编译器版本进行下载,我用的是2010,所以要下载这个. 安装就不用我教了,下面开始看我是如何导入Unity VS的. 点击Import之后我们会发现并没有发生什么,但是接下来我们按一下 ...

  2. 全方位绕过软WAF攻略

    0×00 前言 现在软waf较为多,就在今年夏天苦逼挖洞的日子里经常遇到360主机卫士,安全狗,云锁之类的软waf进行拦截,经常碰到如下拦截提示: 看到以上三个拦截提示就让人头疼不已,欲罢不能. so ...

  3. 蜗牛—Android基础之button监听器

    XML文件中有一个textView 和 一个button. <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  4. linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换

    用域名获取IP地址或者用IP获取域名 #include<stdio.h> #include<sys/socket.h> #include<netdb.h> int ...

  5. mac gem命令

    $ gem sources -r https://rubygems.org/ (移除旧版本的镜像,如果你不知道你电脑上目前用的是什么镜像,可用  $ gem sources -l  来查看)  $ g ...

  6. spring 拦截器简介

    spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...

  7. [Phoenix] 三、DML语法

    摘要: 云HBASE上Phoenix支持的DML语法 从一个或者多个表中查询数据.LIMIT(或者FETCH FIRST) 在ORDER BY子句后将转换为top-N查询. 云HBASE上Phoeni ...

  8. java object monitor

    1 什么是java object monitor 每个java对象头中都有锁状态位标记.java中在使用synchronize同步的时候,肯定是涉及到某个对象的锁.因此,在考虑同步的时候,首先要想到是 ...

  9. Create a /etc/yum.repos.d/mongodb-org-4.0.repo

    Install MongoDB Community Edition on Red Hat Enterprise or CentOS Linux — MongoDB Manual https://doc ...

  10. call by value reference name

    按名调用 Algol 按值调用 Java https://docs.python.org/3.6/faq/programming.html#how-do-i-write-a-function-with ...