Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6361   Accepted: 1378

Description

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)

Source

trie树+贪心

DFS预处理出每个结点到根的路径的异或和。两点之间路径的异或和等于各自到根的路径的异或和的异或。

将所有的异或和转化成二进制串,建成trie树。

对于每个二进制串,在trie树上贪心选取使异或值最大的路径(尽量通往数值相反的结点),记录最优答案。

↑为了防止匹配到自身的一部分,每个二进制串都应该先查完再插入trie树。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,w;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
}
int t[mxn*][],cnt=;
int n,ans=;
int f[mxn];
void insert(int x){
int now=;
for(int i=;i>=;i--){
int v=(x>>i)&;
if(!t[now][v])t[now][v]=++cnt;
now=t[now][v];
}
return;
}
void query(int x){
int now=;
int res=;
for(int i=;i>=;i--){
int v=(x>>i)&;
if(t[now][v^]){
v^=;
res+=(<<i);
}
now=t[now][v];
}
ans=max(res,ans);
return;
}
void DFS(int u,int fa,int num){
f[u]=num;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
DFS(v,u,num^e[i].w);
}
return;
}
void init(){
memset(hd,,sizeof hd);
memset(t,,sizeof t);
// memset(f,0,sizeof f);
mct=;cnt=;ans=;
return;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
int i,j,u,v,w;
for(i=;i<n;i++){
u=read();v=read();w=read();
u++;v++;
add_edge(u,v,w);
add_edge(v,u,w);
}
DFS(,,);
for(i=;i<=n;i++){
// printf("f[%d]:%d\n",i,f[i]);
query(f[i]);
insert(f[i]);
}
printf("%d\n",ans);
}
return ;
}

POJ3764 The xor-longest Path的更多相关文章

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

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

  2. 题解 bzoj1954【Pku3764 The xor – longest Path】

    做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...

  3. Solve Longest Path Problem in linear time

    We know that the longest path problem for general case belongs to the NP-hard category, so there is ...

  4. Why longest path problem doesn't have optimal substructure?

    We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...

  5. 【poj3764】 The xor-longest Path

    http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...

  6. POJ3764,BZOJ1954 The xor-longest Path

    题意 In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of ...

  7. 【poj3764】The xor-longest Path

    The xor-longest Path Description In an edge-weighted tree, the xor-length of a path p is defined as ...

  8. FB面经Prepare: Find Longest Path in a Multi-Tree

    给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径 ...

  9. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  10. Educational DP Contest G - Longest Path (dp,拓扑排序)

    题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int ...

随机推荐

  1. SQL server 数据库备份还原Sql

    /************ 一.数据库备份 ************/ --完整备份默认追加到现有的文件 backup database DBXS To disk='d:\backup\DBXS_fu ...

  2. TinyFrame尾篇:整合Spring AOP实现用户认证

    创建Manager用户验证表 这一篇主要讲解使用AOP对用户操作进行验证,如果通过验证,则继续执行,反之,则不能执行.其思想和上一篇完全一致. 由于需要用到用户认证,所以我们新建一个Manager实体 ...

  3. QT 智能提示设置

    qt5.0的智能提示设置 qt默认的是Ctrl+空格 但这个是切换输入法,用着也不习惯 修改的地方是 工具->选项->环境 键盘选项把CompleteThis修改成自己习惯的快捷键

  4. c:forEach 标签中varStatus的用法

    c:forEach varStatus属性 current 当前这次迭代的(集合中的)项index  当前这次迭代从 0 开始的迭代索引count  当前这次迭代从 1 开始的迭代计数first 用来 ...

  5. 将Microsoft Ajax Minifier集成到VS2013对JS、CSS进行编译时压缩

    在网站发布中,一般要将js,css文件压缩减少体积,以减少在HTTP请求中的流量.将Microsoft Ajax Minifier集成到VS2013中就可以对JS.CSS进行编译时压缩. VS2013 ...

  6. mysql 根据字段重复 删除 保留一条

    delete from TableName where id not in (select minid from (select min(id) as minid from TableName gro ...

  7. DatePicker及其监听

    xml文件: <DatePicker android:id="@+id/datep" android:layout_width="wrap_content" ...

  8. window 安装redis服务、卸载redis服务和启动redis服务

    1.安装redis服务 redis-install.bat 1 echo install redis-server23 D:\redis\redis-server.exe --service-inst ...

  9. [转]制作png格式透明图片的简易方法

    原文地址:http://blog.csdn.net/zhouyingge1104/article/details/24460743 photoshp之类的专业软件太复杂,其实,制作透明图标有比较简易的 ...

  10. .net的Hello World之旅

    class Program    {        //这是主函数,是程序的入口        static void Main(string[] args)        {            ...