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. ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    在用c#生成应用程序的时候,读写dbf时,open方法出错 ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 以前这个程序是用着好 ...

  2. 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列

    package com.wangcf; /** * 把字符串"3,1,2,4"以","分割拆分为数组,数组元素并按从小到大的顺序排列 * @author fan ...

  3. U盘安装OSX

    1.插入U盘,磁盘工具,格式化U盘为Mac OS X拓展 (日志式): 2.去网站搜索recovery disk assistant,此文件大约1.1M,直接打开使用它制作启动盘,进度条完毕就完成了. ...

  4. 团队项目选题报告(I know)

    一.团队成员及分工 团队名称:I know 团队成员: 陈家权:选题报告word撰写 赖晓连:ppt制作,原型设计 雷晶:ppt制作,原型设计 林巧娜:原型设计,博客随笔撰写 庄加鑫:选题报告word ...

  5. Floyd算法(原理|代码实现)

    http://www.cnblogs.com/twjcnblog/archive/2011/09/07/2170306.html 正如我们所知道的,Floyd算法用于求最短路径.Floyd算法可以说是 ...

  6. 关于C语言的问卷调查!!!!!!!!!!

    1.我对自己的未来是现在通过大学这一平台逐渐接触社会,通过大学的这段时间学习C语言等计算机语言技术,有一技之长在手,并且通过大学时间丰富自己的业余生活,加强自己的人脉关系,为未来在事业上的发展做准备! ...

  7. lintcode-407-加一

    407-加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 样例 给定 [1,2,3] 表示 123, 返回 [1,2,4 ...

  8. spring-test与junit

    1.添加依赖 spring-test junit spring-context(自动添加依赖其他所需的spring依赖包) 2.在class前添加以下注解,用于配置xml文件的位置 @RunWith( ...

  9. 对IT行业的看法和对软件工程的理解

    现在社会上IT行业的人才需求越来越大,而作为一个学软件工程的大学生,我认为IT行业是一个前景十分强大的发展方向,而且现在的社会越来越信息化,未来的生活中,电脑肯定是不可缺少的,所以我认为IT行业这是一 ...

  10. 用友 SAP 金蝶 季报

    用友 2018Q3季报 营收:.42亿 营收收入同比增长:42.36% 净利润:.35万 净利润同比增长率:113.83% 销售毛利率:66.88% 销售净利率:19.29% 用友2017财年年报 营 ...