CodeForces 416 B Appleman and Tree DP】的更多相关文章

Appleman and Tree 题解: 定义dp[u][1] 为以u的子树范围内,u这个点已经和某个黑点相连的方案数. dp[u][0] 为在u的子树范围内, u这个点还未和某个黑点相连的方案数. 转移方程: 如果 u为黑点, dp[u][0] = 0, dp[u][1] = 1, 然后考虑从下面转移过来, dp[u][1] *= dp[v][0] + dp[v][1]. 也就是说, 如果 v 点为黑,则切断这个边, 如果v点为白,则不切断, 即对于v来说,每个情况,切边的情况也只有一种,…
题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的max(a[i]) - min(a[i]) <= d. ps.连通子图的定义: 如果一个点集V为一个连通子图,则对于任意两点a,b∈V,有a到b路径上的所有点u∈V. 题解: 因为要保证max(a[i]) - min(a[i]) <= d,所以可以人为地选出一个点rt作为点权最大的点. 这样在求以rt…
题目链接: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…
题目链接 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…
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…
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])\) 再考虑分给其他子树的情况:\(…
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只有22. 对于每一个子树,询问其中最长的,满足:路径上的字符集可以重组成字符串的路径的长度. 题解 明显是用mask维护信息,然后启发式合并一下. 一般启发式合并需要用map维护信息,这样的复杂度是log^2的.如果保留每个点重儿子的信息,就可以用全局变量维护,全局变量的大小就可以开很大,可以做到l…
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3    法I:后序遍历.同Unique Binary Search Trees II,只是现在…