trie--- POJ 3764 The xor-longest Path】的更多相关文章

The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10038   Accepted: 2040 Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: ⊕ is the xor operator. W…
[题目链接] http://poj.org/problem?id=3764 [算法] 首先,我们用Si表示从节点i到根的路径边权异或和 那么,根据异或的性质,我们知道节点u和节点v路径上的边权异或和就是Sx xor Sy 问题就转化为了 : 在若干个数中,找到两个数异或的最大值,可以用Trie树加速,具体细节笔者不再赘述 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <…
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问,要求树上两点之间的权值异或和. \(n,m\le 10^7\) Xor运算有一些很显然的性质: \(a \oplus a = 0\) \(a \oplus b = b \oplus a\) \(a\oplus b\oplus c = a\oplus(b\oplus c)\) 对于这道水题,我们只需要…
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和该怎么求? 令 \(z=\operatorname{lca}(u,v)\) ,则 \(u\) 到 \(v\) 简单路径的异或和可以分成两段求解:一段是 \(z\) 到 \(u\) 简单路径的异或和,一段是 \(z\) 到 \(v\) 简单路径的异或和,二者异或一下即为 \(u\) 到 \(v\) 简…
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根节点路径的亦或和rec,那么随意路径均能够表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大.那么就建立字典树查询就可以. #include <cstdio> #include <cstring> #include <algorithm>…
We know that the longest path problem for general case belongs to the NP-hard category, so there is no known polynomial time solution for it. However, for a special case which is directed acyclic graph, we can solve this problem in linear time. First…
We all know that the shortest path problem has optimal substructure. The reasoning is like below: Supppose we have a path p from node u to v, another node t lies on path p: u->t->v ("->" means a path). We claim that u->t is also a sh…
The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: {xor}length(p)=\oplus{e \in p…
链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11802   Accepted: 2321 Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights o…
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor(0, u)^xor(0, v). 所以如果预处理出0结点到所有结点的xor路径和,问题就转换成了求n个数中取出两个数,使得xor最大. 这个之前用字典树处理过类似问题. 代码: #include <iostream> #include <cstdio> #include <cst…