BZOJ 1954: Pku3764 The xor-longest Path(贪心+trie)
解题思路
\(trie\)的一个比较经典的应用,首先把每个点到根的异或和算出,然后建一棵\(trie\)把所有权值插入到\(Trie\)中,之后枚举所有结点,在\(Trie\)上贪心的跑统计答案,时间复杂度\(O(nlogn)\)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100005;
inline int rd(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
}
int n,head[N],cnt,to[N<<1],nxt[N<<1],val[N<<1];
int tot,ans,res,w[N],rt;
struct Trie{
int ch[N*40][2];
void insert(int &x,int d,int now){
if(!x) x=++tot; if(!d) return;
if((1<<(d-1))&now) insert(ch[x][1],d-1,now);
else insert(ch[x][0],d-1,now);
}
void query(int x,int d,int now){
if(!d) return;
if((1<<(d-1))&now) {
if(ch[x][0]) res|=(1<<(d-1)),query(ch[x][0],d-1,now);
else query(ch[x][1],d-1,now);
}
else {
if(ch[x][1]) res|=(1<<(d-1)),query(ch[x][1],d-1,now);
else query(ch[x][0],d-1,now);
}
}
}tree;
inline void add(int bg,int ed,int w){
to[++cnt]=ed,nxt[cnt]=head[bg],val[cnt]=w,head[bg]=cnt;
}
void dfs(int x,int F){
for(int i=head[x];i;i=nxt[i]){
int u=to[i]; if(u==F) continue;
w[to[i]]=(w[x]^val[i]); dfs(u,x);
}
}
int main(){
n=rd(); int x,y,z;
for(int i=1;i<n;i++){
x=rd(),y=rd(),z=rd();
add(x,y,z),add(y,x,z);
}
dfs(1,0);
for(int i=1;i<=n;i++) tree.insert(rt,31,w[i]);
for(int i=1;i<=n;i++)
res=0,tree.query(rt,31,w[i]),ans=max(ans,res);
printf("%d\n",ans);
return 0;
}
BZOJ 1954: Pku3764 The xor-longest Path(贪心+trie)的更多相关文章
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- bzoj 1954 & poj 3764 The xor-longest Path dfs+Trie
题目大意 给定一棵n个点的带权树,求树上最长的异或和路径 题解 因为\(xor\)操作满足可结合性,所以有 \(a\text{ }xor\text{ }b\text{ }xor\text{ }b = ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- Solve Longest Path Problem in linear time
We know that the longest path problem for general case belongs to the NP-hard category, so there is ...
- Why longest path problem doesn't have optimal substructure?
We all know that the shortest path problem has optimal substructure. The reasoning is like below: Su ...
- 「LOJ#10056」「一本通 2.3 练习 5」The XOR-longest Path (Trie
#10056. 「一本通 2.3 练习 5」The XOR-longest Path 题目描述 原题来自:POJ 3764 给定一棵 nnn 个点的带权树,求树上最长的异或和路径. 输入格式 第一行一 ...
- 51nod1295 XOR key(可持久化trie)
1295 XOR key题目来源: HackerRank基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个长度为N的正整数数组A,再给出Q个查询,每个查 ...
- [多校联考2019(Round 4 T1)][51nod 1295]Xor key(可持久化trie)
[51nod 1295]Xor key(可持久化trie) 题面 给出一个长度为n的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] 至 A[R] ...
- 【BZOJ】1954: Pku3764 The xor-longest Path
[算法]trie树+xor路径 [题解] 套路1:统计从根到每个点的xor路径和,由于xor的自反性,两个点到根的xor路径和异或起来就得到两点间路径和. 然后问题就是找到n个值中异或值最大的两个值, ...
随机推荐
- jmeter之线程组循环次数
有时候压测需要配置并发的持续时间,这个可以在jmeter中线程组页面进行配置 1.jmeter的循环次数2种使用场景 2.持续时间功能介绍 3.持续时间的使用场景 1.jmeter的循环次数2种使用模 ...
- Gogs 安装 - 本地安装,容器安装
文章目录 安装 Gogs 本地安装 前提条件: 数据库 git 创建 git 用户 SSH 服务器 安装 升级 配置及运行 配置 运行 Gogs 服务 在线安装 Gogs 后台运行 gogs 通过 d ...
- 深入浅出WPF(Binding篇1)
Binding在业界的使用一直是音译而来的,称为"Binding".Binding的源是逻辑数据对象,目标则是UI层上面的控件对象.数据通过Binding送达UI层,被UI层展示出 ...
- Ubuntu下apt-get 安装apache2、php、mysql后的默认路径
apache: 采用apt-get 在线安装,安装路径应在/etc/apache2目录下 apache配置文件/etc/apache2/apache2.conf Apache模块路径:/usr/sbi ...
- springboot异步任务、定时任务
打开浏览器 http://localhost:8080/hello ,连续刷新可以看到不会 等待 3秒时间了,pom.xml controller service 代码如下. -----------S ...
- IDEA 光标显示注释
- Eclipse常见版本和JDK常用版本对应关系
Luna 4.4 JDK1.6Mars 4.5 JDK1.7 Neon 4.6 JDK1.8Oxygen 4.7 JDK1.8Photon 4.8 2019年3月
- 常用jQuery技巧总结
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- #python# error:UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30: ordinal not in range(256)
headers={ 'Referer':'https://www.lagou.com/jobs/lis-rds=&fromSearch=true&suginput=', 'User-A ...
- jquery实现按键增加删除css属性(hide)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...