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个值中异或值最大的两个值, ...
随机推荐
- CentOS 7安装图形界面
之前公司的服务器都是用的CentOS 的系统,需要安装图形界面的时候我会执行以下命令 yum -y groupinstall "X Window System" "Fon ...
- virtualenv-windows下排坑
1. 安装 pip install virtualenv pip install virtualenvwrapper-win (win下一定要有这个 -win,不然后续 workon,mkvir ...
- C# 文件、byte相互转换
文件转byte数组: /// <summary> /// 将文件转换为byte数组 /// </summary> /// <param name="path&q ...
- 20190820 On Java8 第十章 接口
第十章 接口 接口和抽象类提供了一种将接口与实现分离的更加结构化的方法. 抽象类和方法 包含抽象方法的类叫做抽象类.如果一个类包含一个或多个抽象方法,那么类本身也必须限定为抽象的,否则,编译器会报错. ...
- 公司C++规范学习
目录 公司C++规范学习 语法部分 风格/约定 公司C++规范学习 语法部分 class和struct关键字的选择:class表示被封装的用户自定义类型,不公开定义非静态数据成员,struct表示数据 ...
- IDF-CTF-cookie欺骗 writeup
题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=40 知识点:base64解码, cookie欺骗 ...
- Communications link failure mysql自动停止 连接拒绝 mysqld dead but sub。。。
服务器环境中 JAVA 连接数据库 Communications link failure, Contection refused 网上很多这种情况,解决基本上是将127.0.0.1换成localho ...
- 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举
https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...
- vue 当前页跳转并强制刷新
watch: { '$route'(to, from) { this.$router.go(0); } }, this.$router.push({ path: '/dashboard/XXZX?' ...
- JS继承——原型链
许多OO语言支持两种继承:接口继承和实现继承.ECMAScript只支持实现继承,且继承实现主要依赖原型链实现. 原型链 基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法. 构造函数.原 ...