【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 个 ...
随机推荐
- k8s1.13.0二进制部署-Dashboard和coredns(五)
部署UI 下载yaml文件https://github.com/kubernetes/kubernetes [root@k8s-master1 ~]# git clone https://github ...
- Oracle 函数使用记录
持续更新…… 参考:https://www.cnblogs.com/bbliutao/archive/2017/11/08/7804263.html 1. ADD_MONTHS 语法: ADD_MON ...
- c++作业:求N的阶乘。
N的阶乘就是n.(n-1)! 5的阶乘是什么?5*4*3*2*1 #include <iostream> using namespace std; int jiecheng(int num ...
- 设置通过Maven创建的工程的JDK的版本,更改conf/settings.xml
eclipse提示警告如下: Build path specifies execution environment J2SE-1.5. There are no JREs installed in t ...
- MHA
MHA 1. MHA简介 1.1 MHA工作原理总结为如下 1.2 MHA工具包介绍 2. 部署MHA 2.1 环境介绍 2.2 一主两从复制搭建 2.3 配置互信 2.4 下载MHA 2.5 安装M ...
- PHP四种序列化方案
原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...
- R-codes-tips
1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2. 载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...
- Letters CodeForces - 978C (二分)
Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...
- ACM-ICPC 2017 Asia Urumqi A. Coins
Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads ...
- poj 2718 切数问题 穷竭搜索
题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列 注意:这个函数必须从小到大才可以排序 ...