当前位置:网站首页>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;
}
边栏推荐
- Count the number of inputs (C language)
- Management and use of DokuWiki
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- Live555 push RTSP audio and video stream summary (I) cross compilation
- Briefly talk about the identification protocol of mobile port -bc1.2
- 每日一题——替换空格
- Some thoughts on extracting perspectives from ealfa and Ebeta
- MySQL MHA high availability cluster
- Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
- Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
猜你喜欢
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
DCDC circuit - function of bootstrap capacitor
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
MHA High available Cluster for MySQL
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Arduino uses nrf24l01+ communication
Sword finger offer 06 Print linked list from end to end
99 multiplication table (C language)
随机推荐
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
实例006:斐波那契数列
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Example 009: pause output for one second
Example 006: Fibonacci series
UE像素流,来颗“减肥药”吧!
關於線性穩壓器的五個設計細節
Working principle and type selection of common mode inductor
Five design details of linear regulator
2020-05-21
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
每日一题——输入一个日期,输出它是该年的第几天
Example 007: copy data from one list to another list.
Shell script
【三层架构】
Carrier period, electrical speed, carrier period variation
leetcode - 445. 两数相加 II
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
亿学学堂给的证券账户安不安全?哪里可以开户
leetcode - 445. Add two numbers II