当前位置:网站首页>Infected Tree(树形dp)
Infected Tree(树形dp)
2022-07-05 08:22:00 【山中一扶苏】
原题链接
题目描述
Misha发现了一棵二叉树,它的顶点编号从到。二叉树是一种包含顶点和边的无环连通双向图。每个顶点最多有一个度,而根顶点是有个数的顶点,它最多有一个度。
不幸的是,树根被感染了。
以下过程发生的次数:
Misha要么选择一个未被感染(也没有被删除)的顶点,并删除它,所有的边都有一个端点在这个顶点,或者什么都不做。
然后,感染扩散到由一条边连接到已感染顶点的每个顶点(所有已感染顶点仍然是感染的)。
由于Misha没有太多时间思考,请告诉他他可以从感染中拯救的最大顶点数是多少(注意删除的顶点不计入保存)
输入样例
4
2
1 2
4
1 2
2 3
2 4
7
1 2
1 5
2 3
2 4
5 6
5 7
15
1 2
2 3
3 4
4 5
4 6
3 7
2 8
1 9
9 10
9 11
10 12
10 13
11 14
11 15
输出样例
0
2
2
10
算法
(树形dp)
本题可以通过维护一个 dp 数组可以快速统计 每一个点作为根节点保留左子树或者保留右子树的最大保留人数 ,从而最后通过 dfs 过程中更新迭代,生成最后需要的答案。
值得注意的是题目的删除点及其子树的顺序是从根到枝叶,但在树形 dp 的统计过程中应该从枝叶到根进行 dp 数组统计,因为最后保留的都是每一层的枝叶,一般的树形 dp 业主要是以这样的方式统计
状态方程:
dp[u][0] = sum[l[u]] - 1 + max(dp[r[u]][1],dp[r[u]][0]);//保留左子树
dp[u][1] = sum[r[u]] - 1 + max(dp[l[u]][1],dp[l[u]][0]);//保留右子树
C++ 代码
时间复杂度 O ( n ) O(n) O(n)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
vector<int> g[N];
int dp[N][2];//每一个点作为根节点保留左子树或者保留右子树的最大保留人数
int sum[N],l[N],r[N];//以u为根节点能够保留的总人数,u节点的左节点与右节点
void pre_dfs(int u,int fa)//预处理
{
sum[u] = 1;
for (int i = 0;i < g[u].size();i ++ ) {
int j = g[u][i];
if (j == fa) continue;
//经典代码,利用dfs的规律分别指定左右节点
if (l[u] == 0) l[u] = j;
else r[u] = j;
pre_dfs(j,u);
sum[u] += sum[j];
}
}
void dfs(int u,int fa)
{
dp[u][1] = dp[u][0] = 0;//初始化
for(int i = 0;i < g[u].size();i ++ ){
int j = g[u][i];
if (j == fa) continue;
dfs(j,u);
//最后进行dp统计
dp[u][0] = sum[l[u]] - 1 + max(dp[r[u]][1],dp[r[u]][0]);//save left subtree
dp[u][1] = sum[r[u]] - 1 + max(dp[l[u]][1],dp[l[u]][0]);//save right subtree
}
}
int main()
{
int t;
cin >> t;
while (t -- ) {
int n;
cin >> n;
int m = n - 1;
for (int i = 1;i <= n;i ++ ) {
sum[i] = l[i] = r[i] = 0;
g[i].clear();
}
for (int i = 0;i < m;i ++ ) {
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pre_dfs(1,1);
dfs(1,1);
cout << max(dp[1][0],dp[1][1]) << '\n';
}
return 0;
}
边栏推荐
- Naming rules for FreeRTOS
- MySQL MHA high availability cluster
- General makefile (I) single C language compilation template
- 剑指 Offer 09. 用两个栈实现队列
- Buildroot system for making raspberry pie cm3
- NTC thermistor application - temperature measurement
- go依赖注入--google开源库wire
- Sql Server的存储过程详解
- Classic application of MOS transistor circuit design (2) - switch circuit design
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
猜你喜欢
![[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code](/img/58/576b6b77509ed7a9bef138f3899e37.jpg)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
![[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture](/img/c5/22c6148873508b9205972e1ad970a3.jpg)
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture

Talk about the circuit use of TVs tube
![Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine](/img/99/21c228ff5de46c4a42b60f989b10e8.jpg)
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
![Halcon's practice based on shape template matching [2]](/img/70/3e905661785e570fb406b8e97d41e6.jpg)
Halcon's practice based on shape template matching [2]

Hardware and software solution of FPGA key chattering elimination

leetcode - 445. 两数相加 II

Classic application of MOS transistor circuit design (2) - switch circuit design

Nb-iot technical summary
随机推荐
[three tier architecture]
STM32 virtualization environment of QEMU
Basic information commands and functions of kernel development
动力电池UL2580测试项目包括哪些
Bluetooth hc-05 pairing process and precautions
Weidongshan Internet of things learning lesson 1
Talk about the circuit use of TVs tube
Some thoughts on extracting perspectives from ealfa and Ebeta
C language # and #
Imx6ull bare metal development learning 1-assembly lit LED
STM32 single chip microcomputer -- volatile keyword
Carrier period, electrical speed, carrier period variation
Step motor generates S-curve upper computer
Shell script
Brief discussion on Buck buck circuit
Management and use of DokuWiki
Tailq of linked list
[three tier architecture and JDBC summary]
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
剑指 Offer 05. 替换空格