CF461B Appleman and Tree (树DP)】的更多相关文章

CF461B Appleman and Tree 传送门 一道比较容易的树形DP. 考虑用\(dp[i][1]\)代表将\(i\)分配给\(i\)的子树内黑点的方案数,\(dp[i][0]\)代表将\(i\)分配给\(i\)的子树之外的黑点的方案数. 首先,考虑分给\(i\)的子树黑点的情况:\(dp[i][1]=\sum _{p\in son_i} dp[p][0]\prod _{t\in son_i,t\neq p} (dp[t][1]+dp[t][0])\) 再考虑分给其他子树的情况:\(…
CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. 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 t…
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then…
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l…
B. 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 are color…
题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\leq 50\) . 分析 考虑枚举钦定一个编号为 \(ans\) 之后以他为根跑一次树dp. 思考一下操作的执行过程.首先,操作连接 \(rt\) 和他的儿子 \(v\) 的边时,必须保留 \(rt\) 的编号,然后合并掉 \(v\) ,可以看成是 \(rt\) 将他的编号传递给了 \(v\) (…
一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0..1个黑色节点的划分方案数. 当节点i为白色时.对于它的每一个孩子的节点处理: 求dp(i, 0)时有: 1,将该节点与孩子节点相连,但要保证孩子节点所在的子树种没有黑色节点: 2,将该节点不与该孩子节点相连.则该孩子节点要保证所在子树种有黑色节点: 即dp(i, 0) = π(dp(j,0 )…
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp[u][1]表示以u为根的子树对父亲的贡献为1 现在假设u为白色,它的子树有x,y,z,那么有 dp[u][1]+=dp[x][1]*dp[y][0]*dp[z][0]+dp[x][0]*dp[y][1]*dp[z][0]+dp[x][0]*dp[y][0]*dp[z][1] dp[u][0]+=d…
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树形成一块不含黑色点的方案数 dp[x][1]表示是以x为根的树形成一块含一个黑色点方案数 //зїеп:1085422276 #include <cstdio> #include <cmath> #include <cstring> #include <ctime&g…
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点个数,2次dfs,一次从底向上,另一次从顶向下. //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include &…