当前位置:网站首页>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;
}
}
边栏推荐
- 【力扣】另一棵树的子树
- 【力扣】翻转二叉树
- filezilla传输虚拟机速度慢解决方法
- Speed sensor signal isolation, acquisition and transformation, sine wave and sawtooth wave signal input, square wave signal output, signal converter
- Qt Creator闪退解决办法
- Wide voltage input high voltage output voltage control type
- QTSS常数
- 自动补全 & (自定义)拼音分词器 & 搜索时注意事项
- Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
- busybox date 日期增加一天明天 网上都是减一天 昨天
猜你喜欢

Hm8203 linear two string charging management controller IC

Introduction to Darwin streaming server

2021 - 09 - 15

5-17陕西科技大学的隐藏学生服务

Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
![Vscode instant English translation plug-in [translation (English Chinese Dictionary)]](/img/f4/9bd90910fef061b423ea8309fab439.png)
Vscode instant English translation plug-in [translation (English Chinese Dictionary)]

Rs-485/232 to 4-20ma/0-10v isolated d/a converter

VSCode 即时英文翻译插件 【翻译(英汉词典)】

Minio installation, deployment and simple use

DSL实现自动补全查询
随机推荐
Introduction to Darwin streaming server
Low power LDO linear regulator IC
vscode one dark和c扩展变量颜色冲突 设置settings.json如下即可
minio基础知识介绍
vscode 配置golang开发环境
2022/07/11 第五小组 丁帅 学习笔记 day04
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
2022/07/09 第五小组 丁帅 学习笔记 day02
[bjoi2019] platoon formation (Group backpack)
busybox date 日期增加一天明天 网上都是减一天 昨天
2022/07/12 学习笔记 (day05)循环
Darwin 反射总结
Computational geometry (2)
2021-11-17 esp32 pin reference
Solve cannot read properties of null (reading 'pickalgorithm')
Baby Ehab Partitions Again(dp,构造,位运算)
WebService接口的创建与实现
MySQL Workbench基本使用【创建一个数据库】
BusyBox 1.21.1 有udpsvd功能 可以编译成功 不干涉本机busybox方法
获取当前年月日、时分秒、星期,并实时更新