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

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

题意:

给出一棵树,求这棵树中的最大的异或路径。

代码:

//预处理出来每个点到根的异或值sxor,然后u,v之间的异或路径值就是sxor[u]^sxor[v],lca就消去了。然后把每个点的sxor值插入字典
//树(二进制字典树),枚举每个点贪心的找他的最大异或路径。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int head[MAXN*+],tot,sxor[MAXN*+],sz,nod[MAXN*+][],ans;
struct Edge
{
int to,w,next;
}edge[MAXN*+];
void init()
{
memset(sxor,,sizeof(sxor));
memset(head,-,sizeof(head));
nod[][]=nod[][]=;
tot=;
sz=;ans=;
}
void add(int x,int y,int z)
{
edge[tot].to=y;
edge[tot].w=z;
edge[tot].next=head[x];
head[x]=tot++;
edge[tot].to=x;
edge[tot].w=z;
edge[tot].next=head[y];
head[y]=tot++;
}
void insert(int x)
{
int rt=;
for(int i=;i>=;i--){
int id=(x>>i)&;
if(nod[rt][id]==){
nod[rt][id]=++sz;
nod[sz][]=nod[sz][]=;
}
rt=nod[rt][id];
}
}
void dfs(int x,int fa,int sum)
{
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y==fa) continue;
sxor[y]=(sum^edge[i].w);
dfs(y,x,sum^edge[i].w);
}
}
void solve(int x)
{
int sum=,rt=;
for(int i=;i>=;i--){
int id=(x>>i)&;
if(nod[rt][!id]){
sum|=(<<i);
rt=nod[rt][!id];
}else if(nod[rt][id]) rt=nod[rt][id];
else break;
}
ans=max(ans,sum);
}
int main()
{
int n;
while(scanf("%d",&n)==){
int x,y,z;
init();
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dfs(,-,);
for(int i=;i<n;i++){
solve(sxor[i]);
insert(sxor[i]);
}
printf("%d\n",ans);
}
return ;
}

poj 3764 字典树的更多相关文章

  1. POJ 2418 字典树

    题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...

  2. POJ 2503 字典树

    题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2]  意思是string2的值为string1. 然后给定一波 ...

  3. POJ 2001 字典树(入门题)

    #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...

  4. POJ 2513 字典树+并查集+欧拉路径

    Description: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木 ...

  5. poj 3764 The xor-longest Path(字典树)

    题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...

  6. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

  7. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  8. POJ 3764 (异或+字典树)

    早就听过用字典树求异或最大值,然而没做过.发现一碰到异或的题就GG,而且因为以前做过的一道类似的题(事实上并不类似)限制了思路,蠢啊= =. 题意:一棵带权的树,求任意两点间路径异或的最大值. 题解: ...

  9. POJ 3764 The xor-longest Path ( 字典树求异或最值 && 异或自反性质 && 好题好思想)

    题意 : 给出一颗无向边构成的树,每一条边都有一个边权,叫你选出一条路,使得此路所有的边的异或值最大. 分析 : 暴力是不可能暴力的,这辈子不可能暴力,那么来冷静分析一下如何去做.假设现在答案的异或值 ...

随机推荐

  1. K-近邻算法入门

    K-近邻算法的直观理解就是:给定一个训练集合,对于新的实例,在训练集合中找到k个与该实例最近的邻居,然后根据“少数服从多数”原则判断该实例归属于哪一类,又称“随大流” K-近邻算法的三大要素:K值得选 ...

  2. 斯坦福大学机器学习(Andrew Ng@2014)--自学笔记

    今天学习Andrew NG老师<机器学习>之6 - 6 - Advanced Optimization,做笔记如下: 用fminunc函数求代价函数最小值,分两步: 1.自定义代价函数 f ...

  3. Pearson Distance

    Pearson Distance: where: 1.  is the covariance 2.  is the standard deviation of 3.  is the standard ...

  4. ES6的新特性(5)——数值的扩展

    数值的扩展 二进制和八进制表示法 ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 0b111110111 === 503 // true 0o767 === ...

  5. 1014-C程序的语法树

  6. 冲刺阶段站立会议每日任务i4

    昨天对小组成员的任务进行了进一步细化分配,今天了解了安卓开发环境的相关知识. 遇到的问题: 没有遇到问题.

  7. 团队项目NABCD

    团队成员及项目简介 团队名:伍陸柒 团队成员: 李  俏(20132912 信1301-2) 郝  颖(20132919 信1301-2)http://www.cnblogs.com/haoying1 ...

  8. nodejs 中on 和 emit

    首先测试用例: var EventEmitter = require('events').EventEmitter var life = new EventEmitter(); // life.on( ...

  9. iOS- 再谈ARC里内存问题,ARC里数组、对象内存得不到释放?

    1.前言      本来以为在改成ARC以后,不再需要考虑内存问题了,可是在实践中还是发现有一些内存问题需要注意,今天我不谈block的循环引用的问题,主要说说一些对象.数组不内存得不到释放的情况. ...

  10. TCP系列47—拥塞控制—10、FACK下的快速恢复与PRR

    一.概述 FACK下的重传我们在之前的重传部分已经进行了介绍,这里简单介绍一下随着FACK提出的拥塞控制算法的改进及随后的进一步改进. 从我们之前介绍的RFC2582和RFC5681中可以看到,快速恢 ...