Codeforces Round #781
C. Tree Infection
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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:

  1. 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.
  2. 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.

Input

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)的更多相关文章

  1. Codeforces Round #527 F - Tree with Maximum Cost /// 树形DP

    题目大意: 给定一棵树 每个点都有点权 每条边的长度都为1 树上一点到另一点的距离为最短路经过的边的长度总和 树上一点到另一点的花费为距离乘另一点的点权 选定一点出发 使得其他点到该点的花费总和是最大 ...

  2. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

  3. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  4. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  5. Codeforces Round #372 (Div. 2)

    Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...

  6. Codeforces Round #270 A~D

    Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...

  7. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  8. CodeForces Round

    CodeForces Round 199 Div2   完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem ...

  9. 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 ...

随机推荐

  1. 想学渗透测试,应该考CISP-PTE还是NISP-PT?|网安伴nisp和cisp

    其实两者都可,但要看考生的实际需求! 为什么说两者都可以? 两个证书都由中国信息安全测评中心颁发,CISP-PTE全称国家注册渗透测试工程师,NISP-PT全称国家信息安全水平考试-渗透测试工程师专项 ...

  2. day30-注解

    Java注解 1.注解的理解 注解(Annotation)也被称为元数据(Metadata),用于修饰解释 包.类.方法.属性.构造器.局部变量等数据信息 和注释一样,注解不影响程序逻辑,但注解可以被 ...

  3. 第七十六篇:ref引用(在vue中引用Dom的方法)

    好家伙, 引子: jQuery简化了程序员操作DOM的过程 vue 优势:MVVM 在vue中,程序员不需要操作DOM.程序员只需要把数据维护好即可!(数据驱动视图) 那么若要在vue中操作dom,这 ...

  4. ubuntu 安装及配置Apache

    一.下载.启动apache2 sudo apt-get install apache2 sudo /etc/init.d/apache2 restart 二.修改配置文件 1.更改网站根目录 sudo ...

  5. VS Code C++ 代码格式化(clang-format)

    --- # 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto Language: Cpp # BasedOnStyle ...

  6. rh358 005 dhcp dhcp6 打印机 ansible配置dhcp和打印机

    部署dhcp服务器 主机发送Discover报文 目标为广播地址 同一网段的dhcp收到报文后,dhcp响应一个offer报文 offer报文:dhcp自己的ip地址.和客户端ip以及使用周期,和客户 ...

  7. python 数据挖掘模块学习

    项目中用到的模块 API # 模块: import pandas as pd import numpy as np from scipy.optimize import curve_fit numpy ...

  8. 【读书笔记】C#高级编程 第二十二章 安全性

    (一)身份验证和授权 安全性的两个基本支柱是身份验证和授权.身份验证是标识用户的过程,授权在验证了所标识用户是否可以访问特性资源之后进行的. 1.标识和Principal 使用标识可以验证运行应用程序 ...

  9. C++ 由快排学习到的的随机数等知识

    起: 力扣的912题 数组排序 ,想着先用快速排序来写写,在实际用c++编写的时候,有一些之前没注意到的细节问题造成了一些麻烦. 912. 排序数组 - 力扣(LeetCode) 快排思想 每次以数组 ...

  10. 2021年3月-第01阶段-Linux基础-Linux系统概念-Linux命令

    Linux系统基本概念 图形界面: Ctrl+Shift +号 //调整命令终端变大 Ctrl - 号 //调整命令终端变小 命令终端: ~ 家目录:用户的私有场所,其他用户不能随便访问 root超级 ...