【bzoj1954】Pku3764 The xor-longest Path Trie树
题目描述
给定一棵n个点的带权树,求树上最长的异或和路径
输入
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.
输出
For each test case output the xor-length of the xor-longest path.
样例输入
4
1 2 3
2 3 4
2 4 6
样例输出
7
题解
Trie树
由于x^x=0,所以树上x和y之间路径的异或和 = x到根路径的异或和 xor y到根路径的异或和。
所以我们先对整棵树进行dfs,求出每个节点到根的路径异或和dis,并加入到Trie树。
然后枚举树上的节点,在Trie树中贪心查询与它异或和最大的数,并加到答案中即可。
#include <cstdio>
#include <algorithm>
#define N 100010
using namespace std;
int head[N] , to[N << 1] , len[N << 1] , next[N << 1] , cnt , v[N] , c[N * 30][2] , tot;
void add(int x , int y , int z)
{
to[++cnt] = y , len[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
}
void dfs(int x , int fa)
{
int i;
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa)
v[to[i]] = v[x] ^ len[i] , dfs(to[i] , x);
}
void insert(int x)
{
int i , p = 0;
bool t;
for(i = 1 << 30 ; i ; i >>= 1)
{
t = x & i;
if(!c[p][t]) c[p][t] = ++tot;
p = c[p][t];
}
}
int query(int x)
{
int i , p = 0 , ans = 0;
bool t;
for(i = 1 << 30 ; i ; i >>= 1)
{
t = x & i;
if(c[p][t ^ 1]) ans += i , p = c[p][t ^ 1];
else p = c[p][t];
}
return ans;
}
int main()
{
int n , i , x , y , z , ans = 0;
scanf("%d" , &n);
for(i = 1 ; i < n ; i ++ ) scanf("%d%d%d" , &x , &y , &z) , add(x , y , z) , add(y , x , z);
dfs(1 , 0);
for(i = 1 ; i <= n ; i ++ ) insert(v[i]);
for(i = 1 ; i <= n ; i ++ ) ans = max(ans , query(v[i]));
printf("%d\n" , ans);
return 0;
}
【bzoj1954】Pku3764 The xor-longest Path Trie树的更多相关文章
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...
- POJ3764 The xor-longest path Trie树
代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- POJ 3764 The xor-longest Path trie树解决位运算贪心
http://poj.org/problem?id=3764 题意 : 一颗树,每个边有个值,在树上找一条简单路径,使得这条路径上的边权异或值最大 先找到所有节点到一点的距离 , 显然dis( x ...
- HDU4825 Xor Sum(贪心+Trie树)
Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...
- poj3764 The XOR Longest Path【dfs】【Trie树】
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10038 Accepted: ...
- The Xor-longest Path(trie树)
题目: #10056. 「一本通 2.3 练习 5」The XOR-longest Path 解析: 做完#10051后就不是很难了 继续利用异或的性质有\(dis(u,v) = dis(1,u)\o ...
- Xor - Trie树
题目描述 求一棵带边权的树的一条最大 Xor 路径的值.这里的"路径"不一定从根到叶子结点,中间一段路径只要满足条件也可以. 输入格式 第一行,一个整数 N ,表示一颗树有 N 个 ...
随机推荐
- docker单主机网络
当你安装Docker时,它会自动创建三个网络.你可以使用以下docker network ls命令列出这些网络: [root@localhost ~]# docker network ls NETWO ...
- JavaScript学习整理(转载)
JavaScript的学习整理(一) 目录: 1.换皮肤功能2.显示/隐藏(点击切换)3.显示/隐藏(onmouseover/onmouseout)4.选项卡5.全选/不选/反选(checkbox)6 ...
- B. Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...
- PAT 乙级 1077
题目 题目地址:PAT 乙级 1077 题解 本题没什么难度,但是要注意细节问题,下面简单来说一下: vector 把输入的学生打分存起来,直接用算法库中的 sort 函数给它们排个序,之后直接剔除首 ...
- 如何用纯 CSS 创作一组昂首阔步的圆点
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ejrMKe 可交互视频 ...
- apply 与 lambda
Python中的lambda和apply用法 https://blog.csdn.net/anshuai_aw1/article/details/82347016
- 20181210(os,os.path,subprocess,configparser,shutil)
1.os模块 os表示操作系统,该模块主要处理与操作系统相关的操作.最常用的是文件操作:打开,读取,删除,复制,重命名. 重点掌握增删改查的函数操作. import os# 获取当前执行文件所在文件夹 ...
- zoj 4056
At 0 second, the LED light is initially off. After BaoBao presses the button 2 times, the LED light ...
- Git for Windows 工具的使用(二)
Git分支 当一个人开发功能A而另一个人开发功能B,之后代码进行整合的时候,使代码既有功能A也有功能B.在Git中,Git给了我们分支的概念. 分支可以使用我们快速的开发协作,并且快速的合并. 分支 ...
- P2598 [ZJOI2009]狼和羊的故事(最小割)
P2598 [ZJOI2009]狼和羊的故事 题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么 ...