[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 ...
随机推荐
- CEPH RGW 设置 user default_placement为ssd-placement,优化100KB-200KB小文件性能,使用户创建的bucket对象放置到 SSD设备的Pool上。
sudo radosgw-admin metadata get user:tuanzi > user.md.json vi user.md.json #to add ssd-placement ...
- Linux基础一
基本命令 useradd xxx 创建一个用户 uname 查看系统架构信息 uname -a 显示详细信息 uname -r 显示内核信息 date 显示当前网络时间 cat ...
- 对HI3531的GPIO使用的再分析
在一个嵌入式系统中使用最多的莫过于 通用输入输出 GPIO口.看到论坛中经常有朋友问海思为什么没有提供GPIO驱动.其实不然. 在海思SDK xxx/osdrv/tools/board_tools/ ...
- HI3531例子程序说明
Hisilicon Hi35xx 样例程序使用说明 1. 样例程序文件结构说明 sample # MPP 样例程序 |-- common # hi35x ...
- solaris启动过程详解
在Sparc平台下,Solaris系统中有一个类似PC BIOS的芯片程序(EEPROM OpenBoot)负责识别分区.文 件系统和加载内核,在Solaris 2.6之后的版本中,默认的内核文件存放 ...
- Error: Dynamic is undefined
1.错误描述 Error: Dynamic is undefined @http://localhost:8080/Query/resource/global/scripts/app.js:149:1 ...
- SecurityError:Error #2048:安全沙箱冲突
1.错误描述 SecurityError:Error #2048:安全沙箱冲突:http://localhost:8080/YHD/flash/YHD.swf 不能从 http://123.89.45 ...
- Vasya and Basketball CodeForces - 493C
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows ...
- form表单的action提交写到js中来,同时onclick事件也写在js中来。其action也可以通过ajax来提交的。
1,html脚本 <body> <div style="display: none;"> <form id="submitForm" ...
- EF6CodeFirst+MVC5+Autofac泛型注册 入门实例
贴一个EF6 CodeFirst模式结合MVC5和Autofac(泛型注册)的一个入门实例 网上类似的例子实在太少,最近自己也有用到这一块的知识,总结了一下,不要让后人踩了自己踩过的坑. 1:新建三个 ...