当前位置:网站首页>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
边栏推荐
- Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
- Getprop property
- Making message board with PHP + MySQL
- leetcode——启发式搜索
- Details related to fingerprint payment
- Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
- 深度学习笔记 —— 物体检测和数据集 + 锚框
- 2022/4/22
- Unity camera rotation with sliding effect (rotation)
- Druid -- JDBC tool class case
猜你喜欢
AQS源码阅读
Details related to fingerprint payment
深度学习笔记 —— 物体检测和数据集 + 锚框
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Innovation training (IX) integration
Learning Android V from scratch - UI
深度学习笔记 —— 数据增广
C language: spoof games
Eight misunderstandings that should be avoided in data visualization
Introduction to raspberry pie 3B - system installation
随机推荐
Analysis of POM files
Alibaba tip: it is better to create threads manually
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
深度学习笔记 —— 物体检测和数据集 + 锚框
深度学习笔记 —— 微调
DIY 一个 Excel 版的子网计算器
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
泰克示波器DPO3054自校准SPC失败维修
Open the past and let's start over.
Installation and deployment of Flink and wordcount test
深度学习笔记 —— 数据增广
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Unity攝像頭跟隨鼠標旋轉
JS generates a specified number of characters according to some words
Agile practice | agile indicators to improve group predictability
C list field sorting contains numbers and characters
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
Better way to read configuration files than properties
General enumeration constant class
Record the ThreadPoolExecutor main thread waiting for sub threads