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. poj-3744-Scout YYF I-矩阵乘法

    f[i]=f[i-1]*p+f[i-2]*(1-p); 正好能够用矩阵加速. . . . #include<stdio.h> #include<string.h> #inclu ...

  2. 关于Gradle配置的小结

    前言 使用 Android Studio 来开发 Android 工程的过程中,接触 Gradle 是不可避免的,比如配置签名.引入依赖等.那么 Gradle 到底是什么东西呢? Gradle 是一个 ...

  3. poj 2528(区间改动+离散化)

    题意:有一个黑板上贴海报.给出每一个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:由于l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题.比方[1,10],[1,4],[ ...

  4. ZipOutputStream 用法 小计

    ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); 构造函数之后 文件就已经创建出来了 只是 0kb s.Write(bu ...

  5. 读《疯狂Java讲义》笔记总结三

    1.初始化块 实际上初始化块是一个假象,使用javac命令编译Java类后,该Java类中的初始化块会消失--初始化块中代码会被 "还原" 到每一个构造器中,且位于构造器全部代码的 ...

  6. website link

    error: template with C linkage http://blog.csdn.net/jiong_1988/article/details/7915420http://velep.c ...

  7. 1.新手上路:Windows下,配置Qt环境

    个人体会: 我最初只是想看看C++除了"黑窗口"之外,怎么才能做一些"更好看的东西".之后在网上看到有人推荐Qt,就看了一下官网(https://www.qt. ...

  8. tomcat 代码集

    Tomcat类是整个tomcat的起点,负责加载所有的配置信息以及把配置信息解析转换成tomcat组件对象. Context addWebapp(String contextPath, String ...

  9. 基于Kubernetes 构建.NET Core技术中台

    今天下午在腾讯云+社区社区分享了<基于Kubernetes 构建.NET Core技术中台>,下面是演讲内容的文字实录. 我们为什么需要中台 我们现在处于企业信息化的新时代.为什么这样说呢 ...

  10. PythonCookBook笔记——迭代器与生成器

    迭代器与生成器 迭代是Python最强大的功能之一,虽然看起来迭代只是处理序列中元素的一种方法,但不仅仅如此. 手动遍历迭代器 想遍历但不想使用for循环. 使用next()方法并在代码中捕获Stop ...