POJ3764 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…
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和该怎么求? 令 \(z=\operatorname{lca}(u,v)\) ,则 \(u\) 到 \(v\) 简单路径的异或和可以分成两段求解:一段是 \(z\) 到 \(u\) 简单路径的异或和,一段是 \(z\) 到 \(v\) 简单路径的异或和,二者异或一下即为 \(u\) 到 \(v\) 简…
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…
http://poj.org/problem?id=3764 (题目链接) 今天的考试题,看到异或就有点虚,根本没往正解上想.. 题意 给出一棵带权树,请找出树上的一条路径,使其边上权值的异或和最大. solution 首先我们考虑从根向下dfs,记录下每个点i到根上权值的异或和${val[i]}$.根据异或和的性质:${x~Xor~y~Xor~y=x}$.所以我们可以由${val}$数组两两组合得出树上任意两点之间路径的异或和.(不理解请自己脑补OvO) 然而这样对于时间复杂度并没有提高,仍然…
题意 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}w(e) \] \(⊕\) is the xor operator. We say a path the xor-longest path if it has the largest xor-length.…
The xor-longest Path 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. We say a path the xor-longest path if it has the largest xor-length. Given an edge-we…
给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径就是:  最高高度+第二高高度,然后return 最高高度 package fbPractise; import java.util.*; class TreeNode { int val; List<TreeNode> children; public TreeNode(int value) {…
应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距离即为树的直径. 证明:假设此树的最长路径是从s到t,我们选择的点为u.反证法:假设第一遍搜到的点是v. 1.v在这条最长路径上,那么dis[u,v]>dis[u,v]+dis[v,s],显然矛盾. 2.v不在这条最长路径上,我们在最长路径上选择一个点为po,则dis[u,v]>dis[u,po]…
题意:给你一张DAG,求图中的最长路径. 题解:用拓扑排序一个点一个点的拿掉,然后dp记录步数即可. 代码: int n,m; int a,b; vector<int> v[N]; int in[N]; int dp[N]; int main() { //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); n=read(); m=read(); for(int i=1;i<=m;++i){ a=read(); b=read(); v[a…