当前位置:网站首页>C. Tree infection (simulation + greed)
C. Tree infection (simulation + greed)
2022-04-23 04:56:00 【to cling】
Codeforces Round #781 (Div. 2)
Solution
Be careful : It's infection before injection .
greedy : First, inject the subtree with the most child nodes , Until all subtrees with child nodes are injected , Reinjection 1 Root node No ( Actually 1 The root node can be understood as a subtree with a child node ,0 It's the parent node ,0 No need to be infected and injected ). After all subtrees have infected nodes , Inject the largest number of child nodes at a time , Then all subtrees infect another node . Keep simulating this process , Until all nodes are infected .
Obvious , Every sub tree needs an injection . Count the number of child nodes of all nodes a [ i ] a[i] a[i]( The root node 1 It's got to be included , Equivalent to a subtree with a child node , Because of the node 1 Also inject alone ), Then start with the tree with the most child nodes , Inject... In turn . After all subtrees have infected child nodes , obviously , For the first i i i ( Subscript from 0 Start ) The number of remaining nodes in the tree is a [ i ] − ( n − i ) a[i] - (n - i) a[i]−(n−i). here , Simulate the remaining child nodes .
Code
const int N = 2e5 + 6;
int a[N];
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
a[i] = 0;
for (int i = 2, x; i <= n; i++)
cin >> x, a[x]++;
vector<int> v;
v.emplace_back(1);
for (int i = 1; i <= n; i++)
if (a[i]) v.emplace_back(a[i]);
sort(all(v), greater<int>());
int ans = sz(v);
for (int i = 0, len = sz(v); i < len; i++)
v[i] -= len - i;
while (!v.empty() && v.back() <= 0) v.pop_back();
while (!v.empty())
{
sort(all(v), greater<int>());
int last = 0;
for (int i = 1, len = sz(v); i < len; i++)
if (v[i] == v[i - 1])
last = i;
else break;
v[last]--;
ans++;
for (int i = 0, len = sz(v); i < len; i++)
v[i]--;
while (!v.empty() && v.back() <= 0) v.pop_back();
}
cout << ans << endl;
//cout << "--------------------\n";
}
return 0;
}
版权声明
本文为[to cling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230455049918.html
边栏推荐
- Use AES encryption - reuse the wisdom of predecessors
- Installation and deployment of Flink and wordcount test
- Sword finger offer: the path with a certain value in the binary tree (backtracking)
- 2022/4/22
- selenium模式下切换窗口,抓取数据的实现
- Innovation training (VII) FBV view & CBV view
- Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
- 拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
- [winui3] write an imitation Explorer file manager
- Open the past and let's start over.
猜你喜欢

Learning Android II from scratch - activity

MySQL - index
![View, modify and delete [database] table](/img/a2/fcb38f2006772a1ec45cab520620ba.png)
View, modify and delete [database] table

Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

Arduino UNO r3+LCD1602+DHT11

PIP3 installation requests Library - the most complete pit sorting

【数据库】MySQL单表查询
![[2021] Spatio-Temporal Graph Contrastive Learning](/img/7d/67a0bfa0adecee24bbe291a25ae906.png)
[2021] Spatio-Temporal Graph Contrastive Learning

DIY is an excel version of subnet calculator
随机推荐
Introduction to load balancing
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Learning Android V from scratch - UI
Record the ThreadPoolExecutor main thread waiting for sub threads
负载均衡简介
Excel protects worksheets and workbooks from damage
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
JS détermine si la chaîne de nombres contient des caractères
redis数据类型有哪些
Unity3D 实用技巧 - 理论知识库(一)
Com alibaba. Common methods of fastjson
深度学习笔记 —— 语义分割和数据集
MySQL - data read / write separation, multi instance
[database] MySQL basic operation (basic operation ~)
Spark optimization
什么是指令周期,机器周期,和时钟周期?
多线程基本概念(并发与并行、线程与进程)和入门案例
leetcode——启发式搜索
PHP counts the number of files in the specified folder
Windows remote connection to redis