[POJ 3764] The xor-longest Path
Description
多组数据
给你一颗树,
然后求一条最长异或路径,
异或路径长度定义为两点间简单路径上所有边权的异或和。
Solution
首先 dfs 一遍,求出所有的点到根节点(随便选一个)的边权的异或和,用 D 数组来存下。
不难发现,树上 x 到 y 的路径上所有边权的 xor 结果就等于 D[x] xor D[y]。这是因为根据 xor 的性质 (a xor a = 0),“ x 到根 ” 和 “ y 到根 ”这两条路径重叠的部分恰好抵消掉。
所以,问题就变成了从 D[1]~D[N] 这 N 个数中选出两个,xor 的结果最大。
可以用 Trie 树来快速求解。
upd:wa了不下十次数组开大点就能A.. Trie的空间是玄学=.=
Code
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 200015
#define int long long
using namespace std;
];
],d[N<<];
int n,cnt,tot,maxn;
struct Edge{
int to,nxt,dis;
}edge[N<<];
struct Trie{
int zero,one;
}trie[N<<];
void add(int x,int y,int z){
edge[++cnt].to=y;
edge[cnt].nxt=head[x];
edge[cnt].dis=z;
head[x]=cnt;
}
void clear(){
tot=maxn=;
memset(d,,sizeof d);
memset(vis,,sizeof vis);
memset(head,,sizeof head);
memset(edge,,sizeof edge);
memset(trie,,sizeof trie);
}
void dfs(int now){
vis[now]=;
for(int i=head[now];i;i=edge[i].nxt){
int to=edge[i].to;
if(vis[to]) continue;
d[to]=d[now]^edge[i].dis;
dfs(to);
}
}
void insert(int x){
;
;~i;i--){
<<i)){
if(!trie[now].one) trie[now].one=++tot;
now=trie[now].one;
}
else{
if(!trie[now].zero) trie[now].zero=++tot;
now=trie[now].zero;
}
}
}
int query(int x){
,sum=;
;~i;i--){
<<i));
if(k){
<<i,now=trie[now].one;
else now=trie[now].zero;
}
else{
<<i,now=trie[now].zero;
else now=trie[now].one;
}
}
return sum;
}
signed main(){
while((scanf("%lld",&n))!=EOF){
clear();
;i<n;i++){
scanf("%lld%lld%lld",&x,&y,&z);
add(x+,y+,z);add(y+,x+,z);
}
dfs();
;i<=n;i++)
maxn=max(maxn,query(d[i])),insert(d[i]);
printf("%lld\n",maxn);
}
;
}
[POJ 3764] The xor-longest Path的更多相关文章
- 【POJ 3764】 The xor-longest path
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就 ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- 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 ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- poj 3764 The xor-longest Path (01 Trie)
链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
随机推荐
- Zookeeper笔记3——原理及其安装使用
Zookeeper到底能干什么? 1.配置管理:这个好理解.分布式系统都有好多机器,Zookeeper提供了这样的一种服务:一种集中管理配置的方法,我们在这个集中的地方修改了配置,所有对这个配置感兴趣 ...
- python产生随机值-random模块
import random产生随机值的模块random.random() #获取一个随机的浮点值;help(random.random) #查看随机范围:0-1;random.uniform(1,10 ...
- http缓存(http caching)
通过使用缓存web网站和web应用的性能能够得到显著的提升.Web caches能够减小延迟和网络流量,从而缩短展示资源所花费的时间. 在http中控制缓存行为的首部字段是Cache-Control, ...
- 【html5】html5离线存储
html5本地存储之离线存储 1.为什么使用离线存储 ①最新的主流的浏览器中都已添加了对HTML5的offline storage功能的支持,HTML5离线存储功能非常强大, 它的作用是:在用户没有与 ...
- JBoss启动项目报错
具体报错如下: WARNING: -logmodule is deprecated. Please use the system property 'java.util.logging.manager ...
- Linux系统挂载NTFS文件系统
今天尝试并成功的将一块500G的移动硬盘挂载到了RHEL5的系统上,甚感欣慰.想到也许以后自己或其他同学们会有类似经历,于是尽量细致的记录于此. 无论是一块安装了Windows/Linu ...
- freemarker.template.TemplateException:Macro has no such argument:params
1.错误描述 freemarker.template.TemplateException:Macro mainSelect has no such argument:params 2.错误原因 在宏定 ...
- CentOS中配置lvm存储
磁盘添加 vmware workstation 虚拟机为例 1.关闭虚拟机,在虚拟机设置中添加3块硬盘. 2.首先创建物理卷 pvcreate /dev/sdb /dev/sdc Physical ...
- 初识Go语言
一.Go语言的主要特性: ① 开放源代码的通用计算机编程语言.开放源代码的软件(以下简称开源软件)更容易被修正和改进. ② 虽为静态类型.编译型的语言,但go语言的语法却趋于脚本化,非常简 ...
- python做基本的图像处理
PIL是python中的图像处理类库,为python提供了基本的图像处理和基本操作.而PIL中最重要的就是Image模块,下面给出具体的例子来理解此模块. 读取一幅图像 我们用Image模块中的ope ...