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 Flask模板jinja2在网页上打印显示16进制数?
问题:Python列表(或者字典等)数据本身是10进制,现在需要以16进制输出显示在网页上 解决: Python Flask框架中 模板jinja2的If 表达式和过滤器 假设我有一个字典index, ...
- count_char
import java.util.Scanner; public class count_char { public static void main(String args[]) { int cou ...
- 使用maven插件生成grpc所需要的Java代码
1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...
- babel配置
首页 首页 首页 博客园 博客园 博客园 联系我 联系我 联系我 demo demo demo GitHub GitHub GitHub 管理 管理 管理 魔魔魔芋芋芋铃铃铃 [02]websto ...
- itop-4412开发板学习-内核信号量
1. 翻翻书看下,linux提供两种信号量,内核信号量,由内核控制路径使用,System V IPC信号量,由用户态进程使用.下面的就是内核部分的信号量.内核信号量类似于自旋锁,当锁关闭着时,不允许内 ...
- 导入execl到数据库mysql
GwykhrenyuankuList <body jwcid="$content$"> <span jwcid="@components/AppBord ...
- 如何在Centos7下升级Apache至最新版本
Apache是使用最广泛的应用部署软件.并且它也是所有服务器的必要组成部分.安装最新版本的apache意味着拥有更多最新的功能和修复了已知的BUG. 介绍 在这篇教程里面,我将会介绍在Centos7下 ...
- Use Matlab though C++
0. Environment Windows 8.1 Pro x64 Matlab R2013a 32-bit (installed in "F:\ProgramFiles_x86\MATL ...
- html5判断设备的动作
相应的事件 deviceorientation事件提供设备的物理方向信息,表示为一系列本地坐标系的旋角. devicemotion事件提供设备的加速信息,表示为定义在设备上的坐标系中的卡尔迪坐标.其还 ...
- [C/C++] new/delete和malloc/free基本区别
/**便于遗忘时复习**/ 区别一:本质 new/delete 在C++中是运算符不是函数,需要编译器支持.malloc/free是库函数,需要头文件支持,在C语言中使用. 区别二:开辟内存大小 用 ...