bzoj1954 The xor-longest path】的更多相关文章

做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和该怎么求? 令 \(z=\operatorname{lca}(u,v)\) ,则 \(u\) 到 \(v\) 简单路径的异或和可以分成两段求解:一段是 \(z\) 到 \(u\) 简单路径的异或和,一段是 \(z\) 到 \(v\) 简单路径的异或和,二者异或一下即为 \(u\) 到 \(v\) 简…
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…
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…
题解: 在树上i到j的异或和可以直接转化为i到根的异或和^j到根的异或和. 所以我们把每个点到根的异或和处理出来放到trie里面,再把每个点放进去跑一遍即可. 代码: #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #inc…
给的多叉树, 找这颗树里面最长的路径长度 解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来. 对于每一个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…
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex…
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex…