当前位置:网站首页>(2022 Niu Ke Duo School 5) D-Birds in the tree (tree DP)
(2022 Niu Ke Duo School 5) D-Birds in the tree (tree DP)
2022-08-02 07:53:00 【AC__dream】
Title: 
Sample input:
710111111 21 3twenty four3 52 64 7Sample output:
28The meaning of the question: Given a tree of n nodes, and the color of each node on the tree is 0 or 1, ask how many subtrees have all the leaves of the same color in the end.
Analysis: Let f[i][0] represent the degree 1 (excluding the root node) in the subtree rooted at the i-th nodeThe number of subtrees whose point color is 0, f[i][1] represents the number of subtrees whose degree is 1 (excluding the root node) in the subtree with the i-th node as the root., first explain the meaning of the array: 
For this image, obviously
f[4][0]=0,f[4][1]=1
f[3][0]=1,f[3][1]=0
f[2][0]=1,f[2][1]=0
And what about point one?Is it the number of legal states, that is, f[1][1]=2, f[1][0]=1, it is not, it is actually f[1][1]=2, f[1][0]=3
Note that our f arrayonly has clear requirements for the color of nodes with degree 1 other than the root node of the current subtree, and there is no requirement for the root node, so 1-2, 1-3 are also considered in f[1][1], of course, this is not legal, but this situation is useful for updating the parent node above, such as 1The parent node of 5 is 5, then if the color of 5 is 0, then a subtree can be formed by 5-1-2, and the color of the node with the satisfaction degree of 1 is the same.Of course, We must subtract the impact of this illegal situation when we count the answers, this illegal situationHow many are there?In fact, statistics are better, because the degree of the root node of the subtree formed in this case is 1, which means that it will only extend in the direction of one of his child nodes. If the color of the root node is 1, then how many child nodes j there areThere will be as many illegal situations as f[j][0]. The sum of f[j][0] of all child nodes j is allis illegal.But you will find that 2-1-3 in the subtree rooted at 1 is a legal subtree, so how do we consider this situation?In fact, it is very simple. For example, for the above figure, the current subtree root node No. 1 node has 3 child nodes, then For each child j can contribute fOne of [j][0] legal schemes, of course, you can also not contribute, so there will be f[j][0]+1 legal schemes in total, then this is the case for all child nodes, we finallyIt only needs to find a product according to the multiplication principle, but this will also bring a problem, such as what if all my child nodes do not contribute?Because we have initialized the record in this case at the beginning, so at this time we need to subtract 1 from the answer, which corresponds to the case where we do not select in all subtrees. Finally, don't forget that we need to count theThe subtree with the current node as the root contains the root node of the current subtree and the degree of the root node of the current subtree is 1. We need to exclude this situation. I have analyzed the calculation method just now. For details, seeCode:
#include#include#include#include#include 边栏推荐
猜你喜欢

MySQL-基础

Compact格式下MySQL的数据如何存储到磁盘
![The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]](/img/14/cd6ed7452230571db2e027f61dbdba.png)
The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]

(2022 Nioke Duo School 5) C-Bit Transmission (Thinking)

OC-错误提示

Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)

电商库存系统的防超卖和高并发扣减方案

jvm 二之 栈帧内部结构

pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies

MySQL-执行流程+缓存+存储引擎
随机推荐
吃透Chisel语言.30.Chisel进阶之通信状态机(二)——FSMD:以Popcount为例
LeetCode 2360. The longest cycle in a graph
【ROS基础】map、odom、base_link、laser 的理解 及其 tf 树的理解
MySQL-底层设置
Introduction to mysql operation (4) ----- data sorting (ascending, descending, multi-field sorting)
MySQL error 1055 solution: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
【请教】SQL语句按列1去重来计算列2之和
【机器学习】课程设计布置:某闯关类手游用户流失预测
论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
敏捷、DevOps和嵌入式系统测试
LeetCode刷题(7)
OC-NSArray
责任链模式(Chain Of Responsibility)
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
埋点开发流程
深度学习网络模型的改进与调整
【网络】IP、子网掩码
(2022牛客多校五)C-Bit Transmission(思维)
MySQL database design specification
sql 远程访问链接服务器