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字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
随机推荐
- Python单例模式的设计与实现【完美版】
目录 1. 按 2. 本文地址 3. 通过继承单例父类来实现 4. 使用装饰器实现 4.1. 懒汉式 4.2. 饿汉式 4.2.1. 未加锁版 4.2.2. 加锁版 1. 按 众所周知,对象是解决继承 ...
- Angular Viewchild undefined
Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...
- [php代码审计] apache 后缀名解析“漏洞”
不能说是漏洞,只是 apache 特性而已. 下面是apache httpd.conf中截取的一段: <IfModule mime_module> # # TypesConfig poi ...
- CSS3 nth-of-type(n)选择器 last-of-type选择器 nth-last-of-type(n)选择器 CSS3 only-child选择器 only-of-type选择器
CSS3 nth-of-type(n)选择器 “:nth-of-type(n)”选择器和“:nth-child(n)”选择器非常类似,不同的是它只计算父元素中指定的某种类型的子元素.当某个元素中的子元 ...
- bzoj4408 [Fjoi 2016]神秘数 & bzoj4299 Codechef FRBSUM 主席树+二分+贪心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4299 https://lydsy.com/JudgeOnline/problem.php?id ...
- 英语单词composing
composing 来源——书籍Python.Crash.Course.2015.11 Using Individual Values from a List You can use individu ...
- 【CF1243B1】Character Swap (Easy Version)【思维】
题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...
- 【HDOJ6666】Quailty and CCPC(模拟)
题意:给出罚时现场赛的题数和罚时,问是否有在金牌线下取整被卡出,四舍五入卡入的队伍 n<=1e5 思路: #include<bits/stdc++.h> using namespac ...
- Flutter端代码
新建一个页面FirstScreen.dartmain.dart改动代码 导入import 'dart:ui' as ui;import 'package:flutter_module/FirstScr ...
- Python 测评工具
开源--Python测评工具 Github仓库 本次实验作业的测评工具仅使用Python语言编写. 程序思路是基于文本的快速匹配. 编译test.py运行 1.GUI界面 GUI界面使用了PyQt5完 ...