【bzoj1954】【The xor-longest Path】【trie树】
发布日期:2021-11-16 15:38:37
浏览次数:29
分类:技术文章
本文共 1130 字,大约阅读时间需要 3 分钟。
Description
给定一棵n个点的带权树,求树上最长的异或和路径
Input
The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.
Output
For each test case output the xor-length of the xor-longest path.
Sample Input
4
1 2 3
2 3 4
2 4 6
1 2 3
2 3 4
2 4 6
Sample Output
7
HINT
The xor-longest path is 1->2->3, which has length 7 (=3 ⊕ 4)
注意:结点下标从1开始到N....题解:两个点到根的异或和异或一下就是这两个点之间的异或和。
所以只要统计出所有点到根的异或和,放到trie树里面。
然后在trie树里面依次查询每个点的最大异或即可。
代码:
#include#include #include #define N 100010using namespace std;int point[N],next[N<<1],x,y,v,cnt,n,ans,a[N];struct use{int st,en,v;}e[N<<1];struct trie{ int ch[N*30][2],cnt; void insert(int x){ int now(0); for (int i=30;i>=0;i--){ int t=x&(1< >=i; if (!ch[now][t]) ch[now][t]=++cnt; now=ch[now][t]; } } int query(int x){ int now(0),temp(0); for (int i=30;i>=0;i--){ int t=x&(1< >=i; if (ch[now][t^1]) temp+=1<
转载地址:https://blog.csdn.net/sunshinezff/article/details/51055426 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月13日 11时49分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
mac使用vim命令修改jar包配置文件内容
2019-04-27
Ubuntu16.04 安装与配置 JDK
2019-04-27
CentOS 7x 防火墙命令使用介绍
2019-04-27
Android中实现播放背景音乐功能
2019-04-27
将 CentOS 7 的本地 yum 源更换成阿里云源
2019-04-27
CentOS 7安装Mongodb并使用Robo 3T远程测试连接
2019-04-27
Shell命令获取服务的进程ID
2019-04-27
4G EPS 第四代移动通信系统
2019-04-27
用 C 语言开发一门编程语言 — 变量元素设计
2019-04-27
Linux 操作系统原理 — 文件系统 — 虚拟文件系统
2019-04-27
Linux 操作系统原理 — 文件系统 — 实现原理
2019-04-27
Kubernetes — 生产环境架构简述
2019-04-27
Kong APIGW — Overview
2019-04-27
FD.io/VPP — L2 vSwitch
2019-04-27
FD.io/VPP — QoS — DPDK Hqos
2019-04-27
Kubernetes — Kubespray 开箱即用的部署工具
2019-04-27
Ansible — Inventory 清单文件
2019-04-27
Openstack组件部署 — Nova overview
2019-04-27
Openstack组件部署 — Nova_安装和配置Controller Node
2019-04-27