当前位置:网站首页>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;
}
边栏推荐
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
- How to copy formatted notepad++ text?
- 【三层架构】
- Take you to understand the working principle of lithium battery protection board
- Cinq détails de conception du régulateur de tension linéaire
- QEMU demo makefile analysis
- Sword finger offer 09 Implementing queues with two stacks
- Use indent to format code
- Naming rules for FreeRTOS
- Briefly talk about the identification protocol of mobile port -bc1.2
猜你喜欢

Example 007: copy data from one list to another list.

实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.

Step motor generates S-curve upper computer

STM32 --- serial port communication
![[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc](/img/9e/2524cbb9b90135c54669ba8d5338b7.jpg)
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
![[three tier architecture]](/img/73/c4c75a453f03830e83cabb0762eb9b.png)
[three tier architecture]

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

STM32 virtualization environment of QEMU

Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?

Working principle and type selection of common mode inductor
随机推荐
Classic application of MOS transistor circuit design (2) - switch circuit design
如何写Cover Letter?
DCDC circuit - function of bootstrap capacitor
Soem EtherCAT source code analysis I (data type definition)
2022.7.4-----leetcode.1200
Weidongshan Internet of things learning lesson 1
99 multiplication table (C language)
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Problem solving: interpreter error: no file or directory
On boost circuit
【三层架构及JDBC总结】
My-basic application 2: my-basic installation and operation
Design a clock frequency division circuit that can be switched arbitrarily
Relationship between line voltage and phase voltage, line current and phase current
Some thoughts on extracting perspectives from ealfa and Ebeta
亿学学堂给的证券账户安不安全?哪里可以开户
[three tier architecture]
How to copy formatted notepad++ text?
Sword finger offer 05 Replace spaces
STM32 --- GPIO configuration & GPIO related library functions