SGU 149. Computer Network( 树形dp )】的更多相关文章

题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码).最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N) ----------------------------------------------------------…
一个比较经典的题型,两次DFS求树上每个点的最远端距离. 参考这里:http://hi.baidu.com/oi_pkqs90/item/914e951c41e7d0ccbf904252 dp[i][0]表示最远端在以 i 为根的子树中的最长长度,dp[i][1]记录最远端在以i为根的子树中的次长长度,dp[i][2]表示最远端不在以 i 为根的子树中的最长长度. 答案即为max( dp[i][0], dp[i][2] ); dp[i][0]和dp[i][1]可以通过一次DFS得到. 再看dp[…
时间限制:0.25s 空间限制:4M: 题意: 给出一颗n(n<=10000)个节点的树,和n-1条边的长度.求出这棵树每个节点到最远节点的距离: Solution: 对于一个节点,我们可以用DFS,在O(n)的时间内求出它的最远节点的距离. 显然对于10000个节点,不可能将每一个节点都这样求. 那么我们来看看,对于一个已经求过的节点我们可以做什么: 假设,有节点k,他有子节点p,两者距离为d 已经求得它的最远节点距离为dis1, 这时对他的子节点p来说,有两种情况: 一种是:p在k的与最远节…
和LightOJ1257一样,之前我用了树分治写了.其实原来这题是道经典的树形DP,感觉这个DP不简单.. dp[0][u]表示以u为根的子树中的结点与u的最远距离 dp[1][u]表示以u为根的子树中的结点与u的次远距离 这两个可以一遍dfs通过儿子结点转移得到.显然dp[0][u]就是u的一个可能的答案,即u往下走的最远距离,还缺一部分就是u往上走的最远距离: dp[2][u]表示u往上走的最远距离 对于这个的转移,分两种情况,是这样的: dp[2][v] = max( dp[0][u]+w…
题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果去掉该边和环上面没有被其他环覆盖的边,那么便分为两部分了. 这样只需要记录每条边被环覆盖了几次即可, 用dp[u]表示u点的父边被覆盖了几次. 每次新加进来一条边(a,b) dp[a] ++ ,dp[b] ++ , dp[lca(a,b)] -= 2; 所有边处理完之后,遍历一边此树,同时转移状态…
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1903 题意 一棵树,根上有VOD站,要求任意叶节点到VOD站的距离不超过k,问最少建新VOD站数. 思路 1. 令vod[i]为节点i到VOD站的最短距离,  注意,这是在以i为根的树上有VOD站的情况下,如果没有,vod[i]就设为非法. 依据树形DP的思路,如果在该…
http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer     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 computers. Each new computer was connected t…
Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 34390    Accepted Submission(s): 5383 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D…
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 computers. Each new computer was connected to one of settled earlier. Managers of school are anxious a…
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…