The xor-longest Path

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 6455 Accepted: 1392

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:

{xor}length(p)=\oplus{e \in p}w(e)

⊕ 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)

/*
Trie树+异或+贪心.
异或性质 a^b=(a^c)^(b^c).
首先dfs出根节点到每个节点的xor.
然后a^b=(a^root)^(b^root).
拆成二进制挂在Trie树上跑.
从高位到低位存
贪心此位为1则看0是否存在
正确性显然.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 100001
using namespace std;
int n,m,dis[MAXN],fa[MAXN],ans,cut,tot,head[MAXN],s[MAXN];
struct edge{int v,next,x;}e[MAXN*2];
struct data{int next[2];}tree[MAXN*16];
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v,int x)
{
e[++cut].v=v;
e[cut].x=x;
e[cut].next=head[u];
head[u]=cut;
}
void add(int x)
{
int now=0,xx;
for(int i=30;i>=0;i--)
{
if(x&(1<<i)) xx=1;
else xx=0;
if(!tree[now].next[xx]) tot++,tree[now].next[xx]=tot;
now=tree[now].next[xx];
}
}
int query(int x)
{
int now=0,xx,num=0;
for(int i=30;i>=0;i--)
{
if(x&(1<<i)) xx=0;
else xx=1;
if(tree[now].next[xx])
{
num|=(1<<i);
now=tree[now].next[xx];
}
else now=tree[now].next[!xx];
}
return num;
}
void dfs(int u,int f,int xo)
{
fa[u]=f;s[u]=xo;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!fa[v]&&v!=f) dfs(v,u,xo^e[i].x);
}
}
int main()
{
int x,y,z;
while(~scanf("%d",&n))
{
ans=0;cut=0;tot=0;
memset(head,0,sizeof head);
memset(fa,0,sizeof fa);
memset(tree,0,sizeof tree);
for(int i=1;i<=n-1;i++)
{
x=read(),y=read(),z=read();
x++,y++;
add(x,y,z),add(y,x,z);
}
dfs(1,1,0);
for(int i=1;i<=n;i++) add(s[i]);
for(int i=1;i<=n;i++) ans=max(ans,query(s[i])); printf("%d\n",ans);
}
return 0;
}

Poj 3764 The xor-longest Path(Trie树+xor+贪心)的更多相关文章

  1. POJ 3764 The xor-longest Path trie树解决位运算贪心

    http://poj.org/problem?id=3764 题意 :  一颗树,每个边有个值,在树上找一条简单路径,使得这条路径上的边权异或值最大 先找到所有节点到一点的距离 , 显然dis( x ...

  2. 【POJ 3764】 The xor-longest path

    [题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...

  3. 【bzoj1954】Pku3764 The xor-longest Path Trie树

    题目描述  给定一棵n个点的带权树,求树上最长的异或和路径 输入 The input contains several test cases. The first line of each test ...

  4. POJ3764 The xor-longest path Trie树

    代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...

  5. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

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

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

  7. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

  8. The Xor-longest Path(trie树)

    题目: #10056. 「一本通 2.3 练习 5」The XOR-longest Path 解析: 做完#10051后就不是很难了 继续利用异或的性质有\(dis(u,v) = dis(1,u)\o ...

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

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

随机推荐

  1. Spring Boot:上传文件大小超限制如何捕获 MaxUploadSizeExceededException 异常

    Spring Boot 默认上传文件大小限制是 1MB,默认单次请求大小是 10MB,超出大小会跑出 MaxUploadSizeExceededException 异常 spring.servlet. ...

  2. 牛客 P21336 和与或 (数位dp)

    大意: 给定数组$R$, 求有多少个数组$A$, 满足$0\le A_i \le R_i$且$A_0+...+A_{N-1}=A_0\space or ...\space or \space A_{N ...

  3. Python利用PIL将数值矩阵转化为图像

    要求:输入一个n*n的矩阵,矩阵包括从-1到1的浮点数,将其转化为可视化图像 调库 from PIL import Image import numpy as np import math 载入图像, ...

  4. JVM描述符标识字符含义

    标识字符 含义 B byte C char D double F float I int J long S short Z boolean V void L 对象类型,如Ljava/lang/Obje ...

  5. [转载]torch参数:torch.backends.cudnn.benchmark的意义

    [转载]torch参数:torch.backends.cudnn.benchmark的意义 来源:https://zhuanlan.zhihu.com/p/73711222 完整版请看原文,这里只截取 ...

  6. ActiveMQ入门系列一:认识并安装ActiveMQ(Windows下)

    一.什么是ActiveMQ 度娘给出的定义: Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件:由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Jav ...

  7. 行级安全(Row

    通过授予和拒绝(Grant/Deny)命令控制用户的权限,只能控制用户对数据库对象的访问权限,这意味着,用户访问的粒度是对象整体,可以是一个数据表,或视图等,用户要么能够访问数据库对象,要么没有权限访 ...

  8. 牛客练习赛48 E 小w的矩阵前k大元素

    E 思路: 优先队列,将迭代器变量作为结构体的变量. 迭代器走的时候只能像一个方向走,另外一个方向只有最开始才走.如下图所示: 如果两个方向同时走,同一个值会被遍历多次,像上图那样就能保证每个位置都走 ...

  9. Codeforces 1175F The Number of Subpermutations

    做法①:RMQ(预处理NLOGN+后续跳跃蜜汁复杂度) 满足题意的区间的条件转换: 1.长度为R-L+1则最大值也为R-L+1 2.区间内的数不重复 当RMQ(L,R)!=R-L+1时 因为已经保证了 ...

  10. Java实验1 - 类的继承(super)- 创建checkaccount继承account

    笔记总结: /** 任务81: 继承性,(降低代码亢余度) * 1.class 子类A Extends 父类B,(private 的内容无法被继承) * 2. 方法可以覆盖(Overrides), 注 ...