当前位置:网站首页>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;
}
边栏推荐
- The firmware of the connected j-link does not support the following memory access
- QEMU demo makefile analysis
- On boost circuit
- 關於線性穩壓器的五個設計細節
- UE像素流,来颗“减肥药”吧!
- Relationship between line voltage and phase voltage, line current and phase current
- QEMU STM32 vscode debugging environment configuration
- List of linked lists
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- Use indent to format code
猜你喜欢

Count the number of inputs (C language)

Stm32--- systick timer
![C WinForm [exit application] - practice 3](/img/25/30c795cc3fa6931eb1d733719d4ad0.jpg)
C WinForm [exit application] - practice 3

QEMU STM32 vscode debugging environment configuration

Sword finger offer 06 Print linked list from end to end

Solutions to compilation warnings in Quartus II
![Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine](/img/55/0f05291755dc1c3c03db8e991a1ba1.jpg)
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine

Installation and use of libjpeg and ligpng

My-basic application 2: my-basic installation and operation

Talk about the circuit use of TVs tube
随机推荐
99 multiplication table (C language)
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
List of linked lists
Hardware 3 -- function of voltage follower
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
[three tier architecture]
Arduino uses nrf24l01+ communication
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
实例010:给人看的时间
Live555 push RTSP audio and video stream summary (I) cross compilation
STM32 single chip microcomputer - external interrupt
FIO测试硬盘性能参数和实例详细总结(附源码)
WiFi wpa_ Detailed description of supplicant hostpad interface
Carrier period, electrical speed, carrier period variation
STM32 virtualization environment of QEMU
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Summary -st2.0 Hall angle estimation
Semiconductor devices (I) PN junction
Some thoughts on extracting perspectives from ealfa and Ebeta
Management and use of DokuWiki (supplementary)