HDU2196 - Computer(树形DP)】的更多相关文章

题目大意 给定一颗n个结点的树,编号为1~n,要求你求出每个结点能到达的最长路径 题解 用动态规划解决的~~~~把1 当成树根,这样就转换成有根树了.我们可以发现,对于每个结点的最长路,要么是从子树得到要么是从父亲得到我们用数组f,g,h分别记录从当前结点到子树能够获得的最大值和第二大值以及从当前结点到祖先能够获得的最大值.我们先进行一次dfs,把f和g值求出来,然后再进行一次dfs求出h的值来,对于h值,得分两种情况,对于当前结点s的h值,如果它的父亲得最长路径没有经过结点s,假设父亲结点为v…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31049    Accepted Submission(s): 3929 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 38417    Accepted Submission(s): 6957 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D…
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权,放在数组cost中 令tree(i)表示以节点i为根的子树 对于节点i,离该节点最远的点要不就是在tree(i)中,要不就是在father(i)上面 令: dp[i][1] : 在子树tree(i)中,离i最远的距离 dp[i][2] : 在子树tree(i)中,离i第二远的距离 (递推的时候需要)…
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问每台电脑和其它电脑的最远距离是多少. 思路:这是一道树形DP的经典题目.须要两次DFS,第一次DFS找到树上全部的节点在不同子树中的最远距离和次远的距离(在递归中进行动态规划就可以),第二次DFS从根向下更新出终于答案.对于每次更新到的节点u,他的最远距离可能是来自u的子树,或者是u的父亲节点的最远…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3075    Accepted Submission(s): 1561 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du…
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functionin…
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description   A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new…
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题:给出一棵树,求每个节点的最远点,每一个节点的最远点有两种可能,一种是向下拓展的最远点,一种是父节点的最远点,那么需要两次dfs即可.一次求出每个节点的最远点和次远点,一次直接计算. #include <queue> #include <cmath> #include <cstd…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2850    Accepted Submission(s): 1450 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D…