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 ...
随机推荐
- python装饰器+递归+冒泡排序
冒泡排序 li = [33, 2, 10, 1,23,23523,5123,4123,1,2,0] for k in range(1,len(li)): for i in range(len(li) ...
- JavaSE基础复习---Class类与反射机制
---恢复内容开始--- 目录: 1.java.lang.class类 2.Java中的反射机制 3.运行时与编译时概念 1. java.lang.class类 Java程序在运行时,Java运行时系 ...
- ADB工具的安装
1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...
- 最短路径算法 2.Dijkstra算法
Dijkstra 算法解决的是带权重的有向图上单源最短路径问题,该算法要求所有边的权重都为非负值.该算法的时间复杂度是O(N2),相比于处理无负权的图时,比Bellmad-Ford算法效率更高. 算法 ...
- Hadoop学习(四) FileSystem Shell命令详解
FileSystem Shell中大多数命令都和unix命令相同,只是两者之间的解释不同,如果你对unix命令有基本的了解,那么对于FileSystem Shell的命令,你将会感到很亲切. appe ...
- Android面试收集录 Android系统的资源+其他
1.Android应用程序的资源是如何存储的,如何使用? res文件夹或者assets文件夹 res目录中的资源在R类中生成一个int变量,然后再布局文件中可以直接使用,在代码中,要getResour ...
- axios应用
Skip to content Features Business Explore Marketplace Pricing Sign in or Sign up Watch929 St ...
- php-configure错误解决
configure: error: libjpeg.(a|so) not foundconfigure: error: libjpeg.(a|so) not foundln -sf libjpeg.s ...
- OpenCV入门:(四:混合两张图片)
1. 原理 对两张图片使用如下公式可以得到两张图片的混合图片, 其中f0(x),f1(x)分别是图片1和图片2同一位置的像素点. 2. OpenCV中的AddWeight函数 函数和参数说明: ) s ...
- (干货分享)mac python+appium环境搭建
因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...