Codeforces Round #781(C. Tree Infection)
1 second
256 megabytes
standard input
standard output
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex vv (different from root) is the previous to vv vertex on the shortest path from the root to the vertex vv. Children of the vertex vv are all vertices for which vv is the parent.
You are given a rooted tree with nn vertices. The vertex 11 is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
- Spreading: for each vertex vv, if at least one child of vv is infected, you can spread the disease by infecting at most one other child of vv of your choice.
- Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1e4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer nn (2≤n≤2e5) — the number of the vertices in the given tree.
The second line of each test case contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn (1≤pi≤n), where pipi is the ancestor of the ii-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of nn over all test cases doesn't exceed 2e5.
1 # include<iostream>
2 # include<algorithm>
3 # include<queue>
4 # define int long long
5 # define endl "\n"
6 using namespace std;
7 const int N = 1000050;
8 int f[N],vis[N];
9
10 void solve(){
11 int n;
12 cin>>n;
13 for(int i = 1;i <= n;++i) f[i] = 0;
14 for(int i = 2;i <= n;++i){
15 int x;
16 cin>>x;
17 f[x]++;
18 }
19 int l = 1,r = n,res = n;
20 while(l <= r){
21 int mid = (l+r)>>1;
22 for(int i = 0;i <= n;++i) vis[i] = 0;
23 priority_queue<pair<int,int> > q;
24
25 for(int i = 1;i <= n;++i) if(f[i]) q.push({f[i],i});
26 q.push({1,0});
27 for(int i = 1;i <= mid;++i){
28 if(q.empty()) break;
29 auto now = q.top();q.pop();
30 if(!vis[now.second]) now.first -=mid - i+1,vis[now.second] = 1;
31 else now.first--;
32 if(now.first > 0) q.push(now);
33 }
34 if(q.empty()) res = mid,r = mid-1;
35 else l = mid+1;
36 }
37 cout<<res<<endl;
38 }
39
40 int tt;
41 signed main(){
42 ios::sync_with_stdio(false);
43 cin.tie(nullptr);
44 cout.tie(nullptr);
45 cin>>tt;
46 while(tt--){
47 solve();
48 }
49
50
51 return 0;
52 }
二分实在是精彩
根据题意将相同父节的个数进排序,每次对个数最多的进行操作
如果尚未被感染则选择注射,如果已经被注射过,则选择传播给相同父节点的子节点
通过priority_queue来进行排序,每次取对头进行操作,通过vis进行对节点(相同父节点)的判断是否被感染过
而二分的是操作次数(二分答案),有两种可能,如果操作次数内使得队列为空说明在当前操作次数可以满足全部感染,继续查找更小的操作次数
否则就不能满足全部感染,查找更大的操作次数
Codeforces Round #781(C. Tree Infection)的更多相关文章
- Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP
题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- Codeforces Round #270 A~D
Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- CodeForces Round
CodeForces Round 199 Div2 完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions # When Who Problem ...
- Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解
今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...
随机推荐
- 基于SpringSecurity的@PreAuthorize实现自定义权限校验方法
一.前言 在我们一般的web系统中必不可少的就是权限的配置,也有经典的RBAC权限模型,是基于角色的权限控制.这是目前最常被开发者使用也是相对易用.通用权限模型.当然SpringSecurity已经实 ...
- Oracle_FDW 使用介绍
本文以例子的形式介绍 KingbaseES(Postgresql)数据库如何通过 oracle_fdw 扩展访问Oracle数据库.以下例子在PG12.3 与 KingbaseES V8R6进行过实际 ...
- Python实践项目——LSB隐写术
此为北京理工大学某专业某学期某课程的某次作业 一.项目背景 1.隐写术 隐写术是一门关于信息隐藏的技巧与科学,所谓信息隐藏指的是不让除预期的接收者之外的任何人知晓信息的传递事件或者信息的内容. 2.L ...
- 聊聊 asp.net core 认证和授权
使用asp.net core 开发应用系统过程中,基本上都会涉及到用户身份的认证,及授权访问控制,因此了解认证和授权流程也相当重要,下面通过分析asp.net core 框架中的认证和授权的源码来分析 ...
- 利用c++编写bp神经网络实现手写数字识别详解
利用c++编写bp神经网络实现手写数字识别 写在前面 从大一入学开始,本菜菜就一直想学习一下神经网络算法,但由于时间和资源所限,一直未展开比较透彻的学习.大二下人工智能课的修习,给了我一个学习的契机. ...
- Spring Boot2配置Swagger2生成API接口文档
一.Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 及时性 (接 ...
- Python实验报告——第4章 序列的应用
实验报告 [实验目的] 1.掌握python中序列及序列的常用操作. 2.根据实际需要选择使用合适的序列类型. [实验条件] 1.PC机或者远程编程环境. [实验内容] 1.完成第四章 序列的应用 实 ...
- Elasticsearch: Ngrams, edge ngrams, and shingles
Ngrams和edge ngrams是在Elasticsearch中标记文本的两种更独特的方式. Ngrams是一种将一个标记分成一个单词的每个部分的多个子字符的方法. ngram和edge ngra ...
- mvn clean package 、mvn clean install、mvn clean deploy的区别与联系
使用的时候首选:mvn clean package mvn clean package依次执行了clean.resources.compile.testResources.testCompile.te ...
- 从nuxt开始的SEO之路
故事从一个"美好"的早上开始...... 大清早的来到公司,打开电脑,emm, 还是熟悉的味道,鱼儿被我摸熟了的味道......就在开始准备一天的摸鱼之旅的时候,一种不详的预感涌上 ...