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. 「LuoguP2365」 任务安排(dp

    题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti. 在每批任务开 ...

  2. 结合Django+celery二次开发定时周期任务

    需求: 前端时间由于开发新上线一大批系统,上完之后没有配套的报表系统.监控,于是乎开发.测试.产品.运营.业务部.财务等等各个部门就跟那饥渴的饿狼一样需要 各种各样的系统数据满足他们.刚开始一天一个还 ...

  3. BZOJ3938:Robot

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  4. AtCoder Regular Contest 068E:Snuke Line

    题目传送门:https://arc068.contest.atcoder.jp/tasks/arc068_c 题目翻译 直线上有\(0-m\)这\(m+1\)个点,一共有\(m\)辆火车.第\(i\) ...

  5. java对世界各个时区(TimeZone)的通用转换处理方法

    在进行国际性软件项目开发的过程中,有时候会碰到一些比较特殊的要求.比如:比如说,你做的是个购物网站(假设服务器放在中国上海),当全世界客户在你的网站上下订单买东西后,往往希望看到客户所在地下单时间,比 ...

  6. maven 整合shh框架的pom.xml文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. npm如何删除node_modules文件夹

    npm install rimraf -g 先安装删除工具,然后使用删除命令 rimraf node_modules

  8. 浅谈HTML移动Web开发(转)

    一.响应式Web设计 PC端常用的两种布局方式就是固定布局和弹性布局,前者设置一个绝大多数电脑能征服显示的固定宽度居中显示,后者则采用百分百. 响应式布局意味着媒体查询,响应式web设计并非新的技术, ...

  9. SeetaFace教程(一) 在 VS 中的编译安装和环境配置

    SeetaFace开源库由FaceDetection.FaceAlignment.FaceIdentification三部分组成.FaceDetection是在一副图片中检测出人脸区域,以一个方形区域 ...

  10. 双击jar不能运行的解决方法

    1.问题描述     使用eclipse导出jar.双击jar文件弹出一个内容为“a java exception has occurred”的错误警告提示! 但是在命令行用 java -jar Em ...