The xor-longest Path

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)

题意:

给你一棵树,求树上异或和最大的路径。

题解:

这题之前索神讲过……所以我竟然还考虑了一会才写

首先看到异或我们不妨顺便总结一下它的性质:

  1. 自反性:x^0=x, x^x=0
  2. 结合性:a^b^c=a^(b^c)

 由这两个性质能组合出一个在OI中常用的技巧:a^b^a^c=b^c

也就是说两段相同的东西(可以是前缀,后缀,区间)异或起来就消掉了。

知道这个性质之后我们还知道一个模型:

对于一个数,想要在$n$个数中找到与它异或和最大的数,我们除了暴力,

也可以将每个数拆成二进制后视作一个字符串建立Trie树,这样就可以做到O(logn)查询。

那么我们再来看一下这道题:树上路径由于有多个点所以不能应用上面的方法。

这个时候应用第一个技巧我们可以发现:Sum(u,v)=Sum(1,u)^Sum(1,v)

那么多个点的路径异或和就转化成了两个点的前缀异或和。

然后直接写就好了。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define MAXN 100005
#define MAXM 500005
#define INF 0x7fffffff
#define ll long long inline int read(){
int x=,f=;
char c=getchar();
for(;!isdigit(c);c=getchar())
if(c=='-')
f=-;
for(;isdigit(c);c=getchar())
x=x*+c-'';
return x*f;
} int N,cnt,hd[MAXN],cst[MAXM<<];
int to[MAXM<<],nxt[MAXM<<];
int num=,sum[MAXN],ch[MAXN*][]; inline void addedge(int u,int v,int w){
to[++cnt]=v,cst[cnt]=w,nxt[cnt]=hd[u],hd[u]=cnt;
to[++cnt]=u,cst[cnt]=w,nxt[cnt]=hd[v],hd[v]=cnt;
} inline void insert(int x){
for(int i=,u=;i>=;i--){
bool c=(x&(<<i))!=;
if(!ch[u][c]) ch[u][c]=++num;
u=ch[u][c];
}
} inline void dfs(int u,int fa,int s){
sum[u]=s; insert(s);
for(int i=hd[u];i;i=nxt[i]){
int v=to[i],w=cst[i];
if(v==fa) continue;
dfs(v,u,s^w);
}
return;
} inline int calc(int x){
int ans=,u=;
for(int i=;i>=;i--){
bool c=(x&(<<i))!=;
if(ch[u][c^]) ans+=(<<i),u=ch[u][c^];
else u=ch[u][c];
}
return ans;
} int main(){
N=read(); int ans=;
for(int i=;i<N;i++){
int u=read(),v=read(),w=read();
addedge(u,v,w);
}
dfs(,,);
for(int i=;i<=N;i++)
ans=max(ans,calc(sum[i]));
printf("%d\n",ans);
return ;
}

【poj3764】The xor-longest Path的更多相关文章

  1. 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元

    [BZOJ2337][HNOI2011]XOR和路径 Description 题解:异或的期望不好搞?我们考虑按位拆分一下. 我们设f[i]表示到达i后,还要走过的路径在当前位上的异或值得期望是多少( ...

  2. 【BZOJ2115】[Wc2011] Xor 高斯消元求线性基+DFS

    [BZOJ2115][Wc2011] Xor Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ...

  3. 【BZOJ4269】再见Xor 高斯消元

    [BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...

  4. 【bzoj4296】再见Xor

    4269: 再见Xor Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 176  Solved: 107[Submit][Status][Discuss ...

  5. 【bzoj2115】[Wc2011] Xor

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2512  Solved: 1049[Submit][Status ...

  6. 【Trie】The XOR Largest Pair

    [题目链接] https://loj.ac/problem/10050 [题意] 给出n个数,其中取出两个数来,让其异或值最大. [题解] 经典的01字典树问题. 首先需要把01字典树建出来. 然后对 ...

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

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

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

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

  9. 【poj3764】 The xor-longest Path

    http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...

随机推荐

  1. centos 7 部署 mysql 报错记录

    1. Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY 这是由于yum安装了旧版本的GPG keys造成的,解决办法就是 引用  rpm --i ...

  2. IDEAL葵花宝典:java代码开发规范插件 checkstyle、visualVM、PMD 插件

    前言: visualVM: 运行java程序的时候启动visualvm,方便查看jvm的情况 比如堆内存大小的分配:某个对象占用了多大的内存,jvm调优必备工具. checkstyle: CheckS ...

  3. python基础-正则1

    什么是正则表达式? 正则表达式是一种小型的\高度专业化的变成语言,主要用于字符串处理 正则表达式是一种通用语言,在python中通过re模块实现,import re 工具:在线正则表达式测试 http ...

  4. 详解C/C++ 编译 g++ gcc 的区别

    我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++, 或者说编译用gcc,链 接用g++,一时也不知哪个说法正确,如果再遇上个ex ...

  5. “libgomp.so.1: version `GOMP_4.0' not found” || “libstdc++.so.6: version `CXXABI_1.3.8' not found”错误

    类似问题还有 'ImportError ../lib/libstdc++.so.6: version `CXXABI_1.3.7' not found (required by xxx)'.      ...

  6. ZJOI2012题解

    t1灾难 给一个食物网 如果一个生物吃的所有东西都灭绝了 它也跟着灭绝 求每个生物灭绝时跟着灭绝的生物数量 支配树裸题,我们先拓扑排序,然后建立一棵树满足一个点灭绝时,有且仅有它的子树跟着灭绝 考虑如 ...

  7. Visual Studio 编译后去掉只读属性

    Visual Studio 编译后去掉只读属性 attrib $(TargetPath) -R attrib $(TargetDir)$(TargetName).pdb -R

  8. 怎么看时序图--nand flash的读操作详解

    出处:http://blog.chinaunix.net/uid-28852942-id-3992727.html 这篇文章不是介绍 nand flash的物理结构和关于nand flash的一些基本 ...

  9. Jenkins远程执行shell出现java: command not found

    之前在使用Jenkins执行远程shell脚本时,出现提示java: command not found:多方查找原因后发现是因为远程执行shell脚本时,不会自动加载环境变量,导致出现此错误,解决方 ...

  10. stm32之通信

    本文提到的内容有以下几个方面: 通信概述 串口通信 I2C通信 CAN通信 SPI通信 I2S通信 USB通信 其他通信 一.通信概述 按照数据传送方式分: 串行通信(一条数据线.适合远距离传输.控制 ...