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. Hibernate3.3.2 手动配置annotation环境

    简单记录Hibernate3.3.2如何快速配置环境 一.下载hibernate-distribution-3.3.2.GA-dist.zip文件,建立User libraries. 打开window ...

  2. 深入理解Message, MessageQueue, Handler和Looper

    做过Android的都知道Message, MessageQueue, Handler和Looper,但知道不代表你理解它们.有时觉得用得很顺手,但Android怎么实现又说不上来,总觉得似懂非懂.不 ...

  3. grootJsAPI文档

    groot.view(name,factory) 用于创建一个modelView对象与指令gt-view对应 参数 用途 name 创建的modelView的名称,用groot.vms[name]可以 ...

  4. [BZOJ2429][HAOI2006]聪明的猴子(MST)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2429 分析:要让最大的最小,所以就是最小生成树上的啦,于是问题就变成了有多少个猴子&g ...

  5. C++ redirect input

    #include<iostream> #include<string> #include<fstream> using namespace std; int mai ...

  6. 【JQuery】 ajax 无效的JSON基元

    [如题]个人理解就是 你向传数据[josn格式]了,但是后台接受确不是json格式的 数据, 贴段代码 var strJson = '{ "usercode": "123 ...

  7. Intent和Intent Filters

    什么是Intent     Intent是android开发中的重要对象,它作为一个信息承载对象存在.     我们可以在使用其他一些组件的时候从Intent获取行为响应的准则(即应该做什么东西,如何 ...

  8. Android 强制竖屏

    一般android 显示内容都有两种实现方式,java代码中实现,xml布局中实现(或者权限管理页面) 直接上代码: java方法 setRequestedOrientation(ActivityIn ...

  9. Listview的onItemClickListener无法响应的解决方法(转)

    转:http://www.cnblogs.com/androidez/archive/2013/03/08/2950425.html 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字 ...

  10. git log 格式化输出

    Git log --graph --pretty=format: '%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)& ...