POJ 3764 The xor-longest Path (01字典树)
<题目链接>
题目大意:
给定一颗$n$个节点$(n\leq10^5)$,有边权的树,其边权$(0\leq w < 2^{31})$。让你求出这棵树上任意两个节点之间的异或最大值。
解题分析:
先用DFS预处理出根节点到所有节点的路径异或值,然后任意两点$u,v$之间的路径异或值就能通过 $(u->rt) xor (v->rt)$的异或值得到,因为根节点到u、v的最近公共祖先的路径被异或了两次,所以能够直接得到u,v之间的异或距离。但是因为节点数量有$10^5$个,直接枚举两两节点肯定超时。用01字典树优化一下,快速找到异或的最大值。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; typedef long long ll; template<typename T>
inline void read(T&x){
x=;int f=;char ch=getchar();
while(ch<'' ||ch>''){ if(ch=='-')f=-; ch=getchar(); }
while(ch>='' && ch<=''){ x=x*+ch-''; ch=getchar(); }
x*=f;
} #define rep(i,s,t) for(int i=s;i<=t;i++)
#define clr(a,b) memset(a,b,sizeof(a))
const int N = 1e5+;
ll nxt[N*][],xoval[N];
int n,rt; struct Edge{int to,nxt;ll w; }e[N<<];
int head[N],cnt,pos; inline void init(){
cnt=;clr(head,-);
pos=;clr(nxt,);clr(xoval,);
}
inline void add(int u,int v,int w){
e[cnt]=(Edge){v,head[u],w};head[u]=cnt++;
}
void dfs(int u,int pre,ll nowval){ //得到根到所有点的异或值
xoval[u]=nowval;
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(v==pre)continue;
dfs(v,u,nowval^e[i].w);
}
}
inline void Insert(ll x){
int now=;
for(int i=;i>=;i--){
int to=(x>>i)&;
if(!nxt[now][to])nxt[now][to]=++pos;
now=nxt[now][to];
//num[now]++;
}
}
/*inline void del(ll x){ //将01字典树中删除x
int now=1;
for(int i=30;i>=0;i--){
int to=(x>>i)&1;
num[now]--;
now=nxt[now][to];
}
}*/
inline ll query(ll x){
int now=;ll ans=;
for(int i=;i>=;i--){
int to=(x>>i)&;
if(nxt[now][to^])now=nxt[now][to^],ans+=(<<i);
else now=nxt[now][to];
}
return ans;
}
int main(){
while(~scanf("%d",&n)){
init();
rep(i,,n-){
int u,v;ll w;read(u);read(v);read(w);
add(u,v,w);add(v,u,w);
}
dfs(,-,);
ll ans=-1e18;
rep(i,,n-){
ll res=query(xoval[i]);
ans=max(ans,res);
Insert(xoval[i]);
}
printf("%lld\n",ans);
}
}
POJ 3764 The xor-longest Path (01字典树)的更多相关文章
- 2014百度之星资格赛—— Xor Sum(01字典树)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- [Hdu4825]Xor Sum(01字典树)
Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- HDU 4825 Xor Sum(01字典树)题解
思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...
- 51nod 1295 XOR key 可持久化01字典树
题意 给出一个长度为\(n\)的正整数数组\(a\),再给出\(q\)个询问,每次询问给出3个数,\(L,R,X(L<=R)\).求\(a[L]\)至\(a[R]\)这\(R-L+1\)个数中, ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
随机推荐
- SQL Server 2008将数据库数据导出到脚本
1.在要到处的数据库上右键 2.选择“任务” 3.选择“生成脚本” 4.选定要导出的数据库 5.在“编写数据的脚本”处选择“True” 6.接下来选定要导出的表,然后选择“完成”
- [转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程
[转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程 [转]超详细MySQL安装及基本使用教程
- idea hibernate console 执行hql报错
报错信息 hql> select a from GDXMZD a[2019-08-29 13:45:01] org.hibernate.service.spi.ServiceException: ...
- php $_SERVER 中的 QUERY_STRING和REQUEST_URI
index.php <?php print_r($_GET); parse_str($_SERVER['QUERY_STRING'],$get); print_r($get); print_r( ...
- python 脚本编译成可执行二进制(exe)
本文python3,pyinstaller也支持py2 cmd下载模块pyinstaller 首先: pip install pyinstaller 其次: cmd下进入需要编译的xxx.py文件目录 ...
- 共享OrCAD9.2pSpice9.2+multisim下载地址
http://pan.baidu.com/s/1dDcfiH7ewb9绿色版,解压后即可用!http://pan.baidu.com/s/1kTG43WFMultisim v11绿色版.7zhttp: ...
- 【leetcode】1079. Letter Tile Possibilities
题目如下: You have a set of tiles, where each tile has one letter tiles[i]printed on it. Return the num ...
- springboot2整合logback.xml动态修改日志打印级别
今天找bug烦到了,生产上的日志级别不能修改,非常不利于排查问题,于是想到了动态修改日志打印级别, 因为上一周把项目升级成springboot2,并且使用logback.xml管理日志打印,所以修改也 ...
- WPF 设置窗体大小为显示器工作区域大小
最近做的项目遇到一个问题,窗体在1680*1050分辨率下显示,系统字体设置为小字体时,显示没问题,但是调到中等字体时,窗体显示位置出了问题.主窗体为无边框窗体,拖动及放大缩小事件都是自己写的. ...
- [CSP-S模拟测试]:降雷皇(DP+树状数组)
题目描述 降雷皇哈蒙很喜欢雷电,他想找到神奇的电光.哈蒙有$n$条导线排成一排,每条导线有一个电阻值,神奇的电光只能从一根导线传到电阻比它大的上面,而且必须从左边向右传导,当然导线不必是连续的.哈蒙想 ...