当前位置:网站首页>Affected tree (tree DP)
Affected tree (tree DP)
2022-07-05 08:28:00 【A Fusu in the mountains】
Original link
Title Description
Misha Found a binary tree , Its vertex numbers are from to . A binary tree is an acyclic connected bidirectional graph containing vertices and edges . Each vertex has at most one degree , And the root vertex is a vertex with a number , It has at most one degree .
Unfortunately , The roots are infected .
The number of times the following process occurs :
Misha Or choose one that is not infected ( It's not deleted ) The summit of , And delete it , All edges have an endpoint at this vertex , Or do nothing .
then , Infection spreads to every vertex connected by an edge to the infected vertex ( All infected vertices are still infected ).
because Misha There is not much time to think , Please tell him the maximum number of vertices he can save from infection ( Note that deleted vertices are not included in the save )
sample input
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
sample output
0
2
2
10
Algorithm
( Tree form dp)
This problem can be solved by maintaining a dp Arrays can be counted quickly Each point is used as the root node to reserve the maximum number of left subtrees or right subtrees , And finally pass dfs Update iterations in the process , Generate the final required answer .
It is worth noting that the order of the deletion point of the title and its subtree is from root to branch , But in the tree dp The statistical process should be carried out from branches and leaves to roots dp Array statistics , Because the last thing left is the branches and leaves of each layer , General tree dp The industry mainly counts in this way
Equation of state :
dp[u][0] = sum[l[u]] - 1 + max(dp[r[u]][1],dp[r[u]][0]);// Keep the left subtree
dp[u][1] = sum[r[u]] - 1 + max(dp[l[u]][1],dp[l[u]][0]);// Keep the right subtree
C++ Code
Time complexity 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];// Each point is used as the root node to reserve the maximum number of left subtrees or right subtrees
int sum[N],l[N],r[N];// With u The total number of people that can be reserved for the root node ,u The left and right nodes of the node
void pre_dfs(int u,int fa)// Preprocessing
{
sum[u] = 1;
for (int i = 0;i < g[u].size();i ++ ) {
int j = g[u][i];
if (j == fa) continue;
// Classic code , utilize dfs The law of specifies the left and right nodes respectively
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;// initialization
for(int i = 0;i < g[u].size();i ++ ){
int j = g[u][i];
if (j == fa) continue;
dfs(j,u);
// The last part dp Statistics
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;
}
边栏推荐
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- Sword finger offer 05 Replace spaces
- 实例008:九九乘法表
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Example 010: time to show
- Ble encryption details
- Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
- Synchronization of QT multithreading
猜你喜欢
NTC thermistor application - temperature measurement
【NOI模拟赛】汁树(树形DP)
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
How to copy formatted notepad++ text?
Summary of SIM card circuit knowledge
STM32 --- serial port communication
Why is 1900 not a leap year
QEMU STM32 vscode debugging environment configuration
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
随机推荐
Sql Server的存儲過程詳解
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
QEMU STM32 vscode debugging environment configuration
Talk about the function of magnetic beads in circuits
每日一题——输入一个日期,输出它是该年的第几天
Explication de la procédure stockée pour SQL Server
99 multiplication table (C language)
剑指 Offer 06. 从尾到头打印链表
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Count the number of inputs (C language)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Arduino uses nrf24l01+ communication
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Shell script realizes the reading of serial port and the parsing of message
剑指 Offer 05. 替换空格
Imx6ull bare metal development learning 1-assembly lit LED
UE pixel stream, come to a "diet pill"!
Sword finger offer 05 Replace spaces
Soem EtherCAT source code analysis I (data type definition)