当前位置:网站首页>C. Tree Infection(模拟+贪心)
C. Tree Infection(模拟+贪心)
2022-04-23 04:55:00 【to cling】
Codeforces Round #781 (Div. 2)
Solution
注意:是先感染后注射。
贪心: 首先注射子节点最多的子树,直到注射完所有的有子节点的子树后,再注射1号根节点(其实1号根节点可以理解为还有一个子节点的子树,0是其父节点,0不需要被感染和注射)。待所有的子树中都有被感染的节点后,每次对目前子节点最多的一个注射,然后所有子树再感染一个节点。一直模拟这个过程,直到全部节点都被感染。
显而易见,每一棵子树都需要注射。统计所有节点的子节点个数 a [ i ] a[i] a[i](根节点 1 也要算进去,相相当于还有一个子节点的子树,因为节点1也要单独注射),然后从子节点最多的一棵树开始,依次注射。所有的子树中均有感染的子节点后,显然,对于第 i i i (下标从0开始)棵子树中剩余的节点数是 a [ i ] − ( n − i ) a[i] - (n - i) a[i]−(n−i)。此时,对剩下的子节点进行模拟操作即可。
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://blog.csdn.net/QQ2530063577/article/details/124319928
边栏推荐
- Druid -- JDBC tool class case
- Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
- Progress of innovation training (IV)
- redis数据类型有哪些
- 【数据库】MySQL单表查询
- 敏捷实践 | 提高小组可预测性的敏捷指标
- PHP counts the number of files in the specified folder
- Knowledge points sorting: ES6
- Leetcode006 -- find the longest common prefix in the string array
- Manually write smart pointer shared_ PTR function
猜你喜欢
【数据库】表的查看、修改和删除
Unity rawimage background seamlessly connected mobile
Arduino UNO r3+LCD1602+DHT11
Practice and exploration of knowledge map visualization technology in meituan
Field injection is not recommended using @ Autowired
【数据库】MySQL基本操作(基操~)
View analysis of scenic spots in ArcGIS
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
DIY 一个 Excel 版的子网计算器
深度学习笔记 —— 微调
随机推荐
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
Eight misunderstandings that should be avoided in data visualization
View, modify and delete [database] table
深度学习笔记 —— 物体检测和数据集 + 锚框
[2021] Spatio-Temporal Graph Contrastive Learning
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
简单的拖拽物体到物品栏
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
The programmer starts the required application with one click of window bat
Innovation training (II) task division
redis数据类型有哪些
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
C list field sorting contains numbers and characters
【数据库】MySQL单表查询
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Arduino UNO r3+LCD1602+DHT11
Wechat payment function
Flink's important basics
Field injection is not recommended using @ Autowired