The xor-longest Path [Trie]
The xor-longest Path
题目描述
给定一棵\(n≤100 000\)个点的带权树,求树上最长的异或和路径。
输入
多组数据。每组数据第一行一个整数n(\(1≤n≤100 00\),接下去n-1行每行三个整数\(u(0≤u<n) ,v(0≤v<n) ,w(0≤w<2^{31})\),表示u和v之间的长度为w的边。
输出
对于每组数据输出结果。
样例输入
4
1 2 3
2 3 4
2 4 6
样例输出
7
题解
其实也是很简单的一道题,因为题目输入是一棵树,有边权,要我们求树上的最长异或和路径,可以想到这样一个性质,一遍深搜处理出每个点到根节点的异或值d[x],那么树上任意两个点的异或和其实就是d[x]^d[y],如果实在不懂可以自己动手画一棵树感受一下,那么问题就转化成了在d[1]~d[n]中任选两个点,求异或值最大,也就是这道题The Xor Largest Pair,剩下的也不用讲了,trie树裸题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define Min(a,b) (a)<(b)?(a):(b)
#define Max(a,b) (a)>(b)?(a):(b)
#define in(i) (i=read())
using namespace std;
int read() {
int ans=0,f=1; char i=getchar();
while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while(i>='0' && i<='9') {ans=(ans<<1)+(ans<<3)+i-'0'; i=getchar();}
return ans*f;
}
int tot,n,cnt,ans;
struct edge {
int to,next,v;
}e[200010];
int head[100010];
int trie[4000010][2],d[100010];
void add(int a,int b,int c) {
e[++cnt].to=b;
e[cnt].v=c;
e[cnt].next=head[a];
head[a]=cnt;
}
void dfs(int u,int fa) {
for(int i=head[u];i;i=e[i].next) {
int to=e[i].to;
if(to!=fa) {
d[to]=(d[u]^e[i].v);
dfs(to,u);
}
}
}
void insert(int x) {
int p=0;
for(int i=31;i>=0;--i) {
int c=(x>>i&1);
if(!trie[p][c]) trie[p][c]=++tot;
p=trie[p][c];
}
}
int find(int x) {
int sum=0,p=0;
for(int i=31;i>=0;--i) {
int c=(x>>i&1),o=(c^1);
if(trie[p][o]) p=trie[p][o],sum=(sum<<1|1);
else p=trie[p][c],sum<<=1;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF) {
ans=tot=cnt=0;
memset(d,0,sizeof(d));
memset(e,0,sizeof(e));
memset(head,0,sizeof(head));
for(int i=1;i<n;++i) {
int u,v,c;
in(u); in(v); in(c);
add(u,v,c); add(v,u,c);
} dfs(1,0);
for(int i=1;i<=n;++i) {
ans=Max(ans,find(d[i]));
insert(d[i]);
}
printf("%d\n",ans);
}
return 0;
}
博主蒟蒻,随意转载.但必须附上原文链接
http://www.cnblogs.com/real-l/
The xor-longest Path [Trie]的更多相关文章
- 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\) 简单路径的异或和 ...
- 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 ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- 【bzoj1954】Pku3764 The xor-longest Path Trie树
题目描述 给定一棵n个点的带权树,求树上最长的异或和路径 输入 The input contains several test cases. The first line of each test ...
- [USACO]6.1.3 cow xor(二进制+Trie)
题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...
- POJ3764 The xor-longest path Trie树
代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...
- 【xsy1147】 异或(xor) 可持久化trie
我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...
随机推荐
- 数据分析处理库Pandas——索引进阶
Series结构 筛选数据 指定值 备注:查找出指定数值的索引和数值. 逻辑运算 备注:查找出值大于2的数据. 复合索引 DataFrame结构 显示指定列 筛选显示 备注:值小于0的显示原值,否则显 ...
- python系列7进程线程和协程
目录 进程 线程 协程 上下文切换 前言:线程和进程的关系图 由下图可知,在每个应用程序执行的过程中,都会去产生一个主进程和主线程来完成工作,当我们需要并发的执行的时候,就会通过主进程去生成一系列的 ...
- clear()、sync()、ignore()
#include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rds ...
- 001---C/S架构
C/S 架构介绍 什么是C/S架构 C:client,客户端 S:server,服务端 实现客户端和服务端之间的网络通信 什么是网络 人与人之间交流是通过语言,才能彼此理解对方的意思.但是地球上有多个 ...
- springboot升级到2.x需要改动的地方
由于需要跟进技术发展的脚步,对原有项目springboot进行2.0升级,但升级并不是说改一下版本就完事了,springboot2.0变动比较多,详细变化可以百度一下,下面针对升级springboot ...
- Android开发——Android手机屏幕适配方案总结
)密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...
- win10 无法修改默认程序 默认打开方式的解决方法
此时是2018年11月24日 win10 pro 64位 版本是1803 具体版本号是17134 情景: 我的状况是.json文件的默认打开方式被新安装的应用霸占了,然后无论是通过“右键-属性-更改 ...
- allegro导入网表过程中出现的错误信息
1. 找不到焊盘PAD,下面这句话的意思是器件封装找不到焊盘46.pad WARNING(SPMHNI-): Unable to load symbol ): Could not find padst ...
- 破解PHPStrom 10 and Pycharm
注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...
- Python 3 学习笔记之——错误和异常
1. 语法错误 Python 的语法错误被称为解析错,语法分析器会指出出错的代码行,并且在最先找到的错误的位置标记一个小小的箭头. >>> while True File " ...