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. Ubuntu—安装python的第三方包gevent

    今晚花很多时间, 使用 sudo pip3 install gevent 但是始终没有成功. 柳暗花明又一村 sudo apt-get install python3-gevent 搞定!!! 人生如 ...

  2. 亚马逊拟斥资15亿美元建航空货运中心 - Amazon to spend $1.49 bln on air cargo hub, fans talk of bigger ambitions - ReutersFebruary 1, 2017

    2月1日消息,亚马逊本周二宣布将在肯塔基州开建其第一个航空货运中心,以应对高速增长的航空货运需求.亚马逊预计,该项目将带来2000个工作岗位. 据悉,该项计划总投入约为15亿美元,亚马逊或可从当地政府 ...

  3. 常用的不易记忆的css自定义代码

    在制作页面时,经常会遇到需要自定义一些标签的默认行为(如:input的占位符等),但这些默认的设置的css一般比较难记住,所以有必要自己做一下记录.下面是我经常用到的一些重设默认行为的css. 1.占 ...

  4. php 中关于pdo的使用

    之前一段时间,开始了php的研究,看了关于PDO的一些资料,发现不错,整理和总结一下,作为开发笔记,留待日后使用,<PHP开发笔记系列(一)-PDO使用>. PDO是PHP Data Ob ...

  5. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 04

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2412 版本控制地址    [https://git.coding.net/ ...

  6. SGU 199 Beautiful People 二维最长递增子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...

  7. jdbc 3.0

    1.将Blob.Clob类型数据保存到数据库 import java.io.File; import java.io.FileInputStream; import java.io.FileReade ...

  8. Node.js系列——(2)发起get/post请求

    服务器与浏览器的交互主要方式有get/post请求. 下面,我们来看一下node.js发起get/post请求. 1.get 由于get请求的参数在url后面,所以相对比较简单.node.js中的ur ...

  9. Navicat Premium_11.2.7简体中文版 破解版本 windows版本

    亲测可用 自己一直在用的 https://pan.baidu.com/s/1VVKKQoIKVB0BgNXBK4YTrQ

  10. linux自启动、定时启动脚本

    linux开机自启动 想让一个程序.脚本开机自启动,可以在/etc/rc.d目录下面找到rc.local文件,编辑该文件,在尾部加上需要运行的命令即可. 如: #cd /etc/rc.d #sudo ...