当前位置:网站首页>Decorate Apple Tree
Decorate Apple Tree
2022-07-19 06:06:00 【lovesickman】
Decorate Apple Tree
Topic translation
The main idea of the topic
To give you one n n n Nodes with 1 1 1 A tree for roots , To give the n n n Random coloring of nodes , Define a point as a happy node if and only if all points on the subtree of this node have different colors . Find out for 1 ∼ n 1\sim n 1∼n Each of them k k k, The number of happy knots is greater than or equal to k k k The minimum number of colors required .
Input format
Number one on the first line n ( 1 ≤ n ≤ 1 0 5 ) n(1\le n\le 10^5) n(1≤n≤105), Indicates the number of nodes
The second line n − 1 n-1 n−1 Number p i p_i pi, It means the first one i i i Parent node of nodes ( 1 ≤ p i < i ) (1\le p_i<i) (1≤pi<i)
Output format
a line , n n n Number , For 1 ∼ n 1\sim n 1∼n Each of them k k k, The number of happy nodes is greater than or equal to k k k The minimum number of colors required for
Examples #1
The sample input #1
3
1 1
Sample output #1
1 1 2
Examples #2
The sample input #2
5
1 1 3 3
Sample output #2
1 1 1 2 3
Can't think of , Notice that the number of leaf nodes in a given tree will be output 1, The last number is in 1 Is the maximum value of the leaf node of the subtree of the root , Then I couldn't think of it .
Caption , The problem can be transformed into how many leaf nodes there are in the subtree rooted at each point , Sorting is the answer .
Don't understand , I know I read others' explanations
Give a tree , Can dye leaves with different colors , Define a node as happy If and only if the leaf node of the subtree ( It can include itself ) The colors are different , Then ask for the difference 1 To n individual happy The minimum number of colors in the case of nodes
In turn to , Think about it first n The situation of , want n individual happy node , That is, all leaves are dyed in different colors , Then consider n-1 The situation of , Just remove a root node , Then it becomes two independent subtrees , Just make sure that the leaves of the subtree with many leaf nodes are dyed with different colors , No matter what color the leaves of other brother trees are dyed, they will not exceed the number of colors of this one , So it is actually the subtree with the second largest number of leaves , So the answer is dfs Preprocess the number of leaf nodes of the subtree corresponding to each node once , Sort the output
( A question that comes to mind ) How to find 1 ~ n The number of nodes of the largest subtree rooted at each point in . Excavation without filling
How to use tape int As a return value dfs Seeking for u How many leaf nodes does the root subtree have ?
Two completely equivalent expressions , I always feel that those without return values are easier to write than those with return values .
int dfs(int u,int fa){
bool flag = 1;
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(e[i]==fa)continue;
flag = 0;
cnt[u] += dfs(j,u);
}
if(flag){
cnt[u] = 1;
}
return cnt[u];
}
void dfs(int u,int fa){
bool flag = 1;
for(int i=h[u];i!=-1;i=ne[i]){
int j = e[i];
if(e[i]==fa)continue;
flag = 0;
dfs(j,u);
cnt[u]+=cnt[e[i]];
}
if(flag){
cnt[u] = 1;
}
}
边栏推荐
- Isolate 4-20mA or 0-20mA signal transmission
- 數學基礎課2_歐拉函數,線性篩,擴歐
- Peerless good problem (bit operation optimization DP)
- [USACO06DEC]The Fewest Coins G(混合背包)
- 开源在线的MarkDown编辑器 --【Editor.md】
- Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
- [simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning
- 4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter
- 2021-09-15
- Computational geometry (2)
猜你喜欢

3.7V lithium battery boost to 5v1a, fs2114 boost conversion chip design layout

數學基礎課2_歐拉函數,線性篩,擴歐
![Chrome browser settings [display the translation language icon in the upper right corner]](/img/87/018e0ab92f698b77ada98ef555bba8.png)
Chrome browser settings [display the translation language icon in the upper right corner]

2022/07/11 第五小组 丁帅 学习笔记 day04

RS-485/232转4-20mA/0-10V隔离D/A转换器

Fs4061a (5V USB input, double lithium battery series application, 5V boost charging, 8.4v Management IC

DSL实现Bucket聚合
![[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning](/img/65/fbb975491d4abd5d1babdf000513e2.png)
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning

【力扣】括号匹配

2022/07/10 第五小组 丁帅 学习笔记 day03
随机推荐
绝世好题(位运算优化dp)
Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle
busybox date 时间的加减
Solve cannot read properties of null (reading 'pickalgorithm')
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V
计算几何(4.17)
It4058a single lithium ion battery charging management
结合图片看常用串口通信UART
5-17陕西科技大学的隐藏学生服务
uboot 编译前的配置命令make config分析
开源在线的MarkDown编辑器 --【Editor.md】
4-20mA to 0-5khz, 5V pulse converter
Qtss data type
數學基礎課2_歐拉函數,線性篩,擴歐
DSL实现Metrics 聚合
Proportional valve amplifier 1a, 2a, 3a, 5A proportional valve drive module 0-10V to 0-24v
DSL实现Bucket聚合
升高压模块隔离模块HRA2460D-2W
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)