【poj3764】The xor-longest Path
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
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)
题意:
给你一棵树,求树上异或和最大的路径。
题解:
这题之前索神讲过……所以我竟然还考虑了一会才写
首先看到异或我们不妨顺便总结一下它的性质:
- 自反性:x^0=x, x^x=0
- 结合性: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的更多相关文章
- 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元
[BZOJ2337][HNOI2011]XOR和路径 Description 题解:异或的期望不好搞?我们考虑按位拆分一下. 我们设f[i]表示到达i后,还要走过的路径在当前位上的异或值得期望是多少( ...
- 【BZOJ2115】[Wc2011] Xor 高斯消元求线性基+DFS
[BZOJ2115][Wc2011] Xor Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ...
- 【BZOJ4269】再见Xor 高斯消元
[BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...
- 【bzoj4296】再见Xor
4269: 再见Xor Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 176 Solved: 107[Submit][Status][Discuss ...
- 【bzoj2115】[Wc2011] Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2512 Solved: 1049[Submit][Status ...
- 【Trie】The XOR Largest Pair
[题目链接] https://loj.ac/problem/10050 [题意] 给出n个数,其中取出两个数来,让其异或值最大. [题解] 经典的01字典树问题. 首先需要把01字典树建出来. 然后对 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- 【poj3764】 The xor-longest Path
http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. ...
随机推荐
- Zookeeper- Error contacting service. It is probably not running解决方案和原理
搭建启动Zookeeper集群出现Error contacting service. It is probably not running解决方案和原理 1.关闭防火墙 [root@srv01 bi ...
- BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...
- Zookeeper用来干什么?
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- JDK中主要的包介绍
- OpenCV——PS 滤镜算法之极坐标变换到平面坐标
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- AtCoder AGC #2 Virtual Participation
在知乎上听zzx大佬说AGC练智商...于是试了一下 A.Range Product 给$a$,$b$,求$\prod^{b}_{i=a}i$是正数,负数还是$0$ ...不写了 B.Box and ...
- HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)
描述 给一个整数N,请你求出以N为分母的最简(既约)真分数中第K小的是多少? 输入 两个整数N个K. 对于30%的数据,1 <= N <= 1000000 对于100%的数据,1 < ...
- 「LuoguP1144」 最短路计数(dijkstra
题目描述 给出一个NN个顶点MM条边的无向无权图,顶点编号为1-N1−N.问从顶点11开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 第一行包含22个正整数N,MN,M,为图的顶点数与边 ...
- C++ ADO连接
#include "stdafx.h" #include <iostream> #include <iomanip> #include "wind ...
- python快速上手教程
python版本 python目前的版本分为2.7和3.5,两种版本的代码目前无法兼容,查看python版本号: python --version 基本数据类型 数字类型 整型和浮点型数据和其它编程语 ...