Codeforces 804D Expected diameter of a tree】的更多相关文章

D. Expected diameter of a tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they…
题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. 我们对每棵独立的树,对于这棵树的每一个点$u$,求出$f[u]$ $f[u]$为这棵树上离$u$最远的点到$u$的距离. 同时我们求出每棵树上的树的直径的长度 现在合并两棵树$A$和$B$的时候,合成的新的树的直径$C$其实有三种情况. 对$A$树中的某点$x$,$B$树中的某点$y$ 1.可能是…
[题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连, 问你此时相连出的树的直径期望是多少?(如果本身就在同一个连通块内,则输出-1) [题解] 我们利用树形dp记录每个点属于的连通块, 以及每个点到不同分支最远点的距离,记为mxd[i] 一遍搜索计算出向下最远,再次搜索的时候得到向上最远即可. 得到各个分支的最远距离之后,我们将其进…
Expected diameter of a tree 我们先两次dfs计算出每个点能到达最远点的距离. 暴力计算两棵树x, y连边直径的期望很好求, 我们假设SZ(x) < SZ(y) 我们枚举 x 的每个端点, 二分找到分界点, 复杂度为SZ(x) * log(SZ(y)) 其实我们对于每次询问我们记忆化一下就可以啦. 这是因为对于SZ(x)小于 sqrt(n)的询问, 我们直接暴力求就好啦, 复杂度q * SZ(x) * log(SZ(y)) 对于SZ(x) > sqrt(n) 这样的…
题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意一点出发搜到最远的一点\(x\),再从\(x\)出发搜到距\(x\)最远的一点\(y\),那么\(d(x,y)\)就是树的直径.时间复杂度为\(O(n)\). 求构成新树的直径 假设原来两棵树的直径分别问\(d_1,d_2\) 令\(f_i\)为点\(i\)所在子树中距它最远的点的距离 新树的直径要…
LINK:Expected diameter of a tree 1e5 带根号log 竟然能跑过! 容易想到每次连接两个联通快 快速求出直径 其实是 \(max(D1,D2,f_x+f_y+1)\) 其中\(D1,D2\)分别为两个联通块内的直径. \(f_x\)表示 从x出发的最长链. 这样容易想到 枚举一个块的点 然后其实要找到 \(C=max(D1,D2)\) 第一个位置满足\(>C-f_x-1\) 然后就能统计答案了. 排序后扫描 复杂度要高 不如排序后二分. 然后加一个记忆化就过了.…
题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通块的直径和各点到连通块内一点的最远距离d[x](树形dp+换根),询问若在同一块内输出-1,否则若随机选出两点x,y,直径为max(d[x]+d[y]+1,x所在块直径,y所在块直径),我们把同一连通块内的d排序,枚举小的连通块中的d,到大的连通块中二分d[x]+d[y]+1<=max(x所在块直径,y…
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! 题目链接:codeforces804D 正解:二分$+DP$ 解题报告: 预处理出每个点所在的树内的最远点距离dis,考虑对于树$A$的点$x$,与树$B$的$y$产生的贡献就是$dis[x]+dis[y]+1$和两棵树的直径取一个$max$. 对于$ma…
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下的两个操作之一: 1.将标记移动至当前节点的子树中的某一个叶子 2.将当前标记向上移,向上移的距离不得超过\(k\) 求最多可以访问到多少个叶子结点 分析:一看就知道应该用树形dp去维护它 我们记\(dp[u]\)表示以\(u\)为根节点的子树中最多可以访问多少个叶子结点 \(dp[u]\)由两部分…
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大path和,这个节点的左右子树必定是纯左右树(即没有拐点), 用另一个参数保留整个二叉树的最大path和,然后计算每一个以当前节点为拐点的路径和并且不断更新整个二叉树的最大值 函数的返回值是纯左右子树的最大path,没有拐点 这个题目给root定位为非空,所以直接这样写可以.如果root为空,这样写就会…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Given a binary…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上的每条路径都是单调或者单峰的 所以我们考虑DP一个每条路径都是单调或单峰的树出来 所以考虑DPf[i][j]" role="presentation" style="position: relative;">f[i][j]f[i][j]表示大小是i的子树…
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a bi…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass…
Description Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Giv…
Level:   Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Exam…
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/problem/F Solution 设\(v_i\)表示第\(i\)个点的果子数,设\(b_i=v_i-\sum_{x\in son}v_x\),显然依题意要满足\(b_i\geqslant 0\). 根据差分的性质我们可以得到\(\sum b_i=x\). 假设我们硬点树上剩下了\(m\)个点,则…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这个值作为一个候选值,然后再对左右子结点分别调用求直径对递归函数,这三个值相互比较,取最大的值更新结果res,因为直径不一定会经过根结点,所以才要对左右子结点再分别算一次.为了减少重复计算,我们用哈希表建立每个结点和其深度之间的映射,这样某个结点的深度之前计算过了,就不用再次计算了. solution…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t…
题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a bina…
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode.com/problems/diameter-of-binary-tree/#/description 题目描述 Given a binary tree, you need to compute the length of the diameter of the tree. The diameter…