D. Bear and Tree Jumps   A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them. Limak is a little polar bear. He lives in a tree that consists of n vertices, num…
题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\limits_{s<t}f(s,t)\) 分析: 注意到\(k\)很小,为了方便转移,定义: \(sz(u,i)\)为\(u\)的子树中与\(u\)的距离模\(k\)等于\(i\)的孩子节点的个数 \(d(u,i)\)为从\(u\)跳到\(sz(u,i)\)对应的节点的跳数之和 转移: \(sz(u,(i…
题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离,$k$为输入的$k$值. $f[i][j]$表示以$i$为根结点,深度对$k$取模为$j$的点的个数. 状态转移时$f[x][i]$一边更新一边和刚刚计算出的$f[u][j]$统计答案. 具体细节可以看代码. #include <bits/stdc++.h> using namespace std…
题目描述 A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them. Limak is a little polar bear. He lives in a tree that consists of n vertices, numbered 1 through n. L…
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means…
个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 158 Solved: 96 [Submit][Status][Discuss] Description 红黑树是一类特殊的二叉搜索树,其中每个结点被染成红色或黑色.若将二叉搜索树结点中的空指针看作是指向一个空结点,则称这类空结点为二叉搜索树的前端结点.并规定所有前端结点的高度为-1. 一棵红黑树是满…
题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input :standard input output:standard output Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices a…
Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1539    Accepted Submission(s): 616 Sample Input 3 1 2 1 1 2 2 3 6 1 2 1 3 2 1 1 2 1 3 2 4 2 5 3 6 Sample Output Case #1: 6 Case #…
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数 转移就是左子树若干个,右子树若干个转移.. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN…
本文出自   http://blog.csdn.net/shuangde800 --------------------------------------------------------------------------------- 题目链接:  url-1018 题意 给一棵边有权值的二叉树,节点编号为1-n,1是根节点.求砍掉一些边,只保留q条边,这q条边构成的子树    的根节点要求是1,问这颗子树的最大权值是多少? 思路 非常经典的一道树形dp题,根据我目前做过的题来看,有多道…