CF1914F.Programming Competition
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
BerSoft is the biggest IT corporation in Berland. There are n employees at BerSoft company, numbered from 1 to n.
The first employee is the head of the company, and he does not have any superiors. Every other employee i has exactly one direct superior pi.
Employee x is considered to be a superior (direct or indirect) of employee y if one of the following conditions holds:
- employee x is the direct superior of employee y;
- employee x is a superior of the direct superior of employee y.
The structure of BerSoft is organized in such a way that the head of the company is superior of every employee.
A programming competition is going to be held soon. Two-person teams should be created for this purpose. However, if one employee in a team is the superior of another, they are uncomfortable together. So, teams of two people should be created so that no one is the superior of the other. Note that no employee can participate in more than one team.
Your task is to calculate the maximum possible number of teams according to the aforementioned rules.
BerSoft 是 Berland 国最大的 IT 公司。该公司共有 n 名员工,编号从 1 到 n。
第 1 号员工是公司负责人,他没有上级。其余每位员工 i 恰好有一位直属上级 pi。
若满足以下任一条件,则称员工 x 是员工 y 的上级(可以是直属上级,也可以是间接上级):
- 员工 x 是员工 y 的直属上级;
- 员工 x 是员工 y 的直属上级的上级。
BerSoft 的组织结构保证:公司负责人是所有员工的上级(直接或间接)。
一场编程竞赛即将举行,需为此组建两人团队。然而,若某团队中一名员工是另一名员工的上级,则他们共处会感到不适。因此,所组建的两人团队必须满足:其中任意一人均不是另一人的上级。注意:每名员工至多只能参加一个团队。
你的任务是:根据上述规则,计算最多可组建多少个团队。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of employees.
The second line contains n−1 integers p2,p3,…,pn (1≤pi≤n), where pi is the index of the direct superior of the i-th employee.
The sum of n over all test cases doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 员工的数量。
每个测试用例的第二行包含 n−1 个整数 p2,p3,…,pn(1≤pi≤n),其中 pi 表示第 i 位员工的直属上级的编号。
所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print a single integer — the maximum possible number of teams according to the aforementioned rules.
对于每个测试用例,输出一个整数——即根据上述规则所能组成的最多队伍数量。
输入输出样例
输入#1
6 4 1 2 1 2 1 5 5 5 5 1 7 1 2 1 1 3 3 7 1 1 3 2 2 4 7 1 2 1 1 1 3
输出#1
1 0 1 3 3 3
说明/提示
In the first test case, team (3,4) can be created.
In the second test case, no team can be created, because there are only 2 employees and one is the superior of another.
In the third test case, team (2,3) can be created.
In the fourth test case, teams (2,4), (3,5) and (6,7) can be created.
In the fifth test case, teams (2,3), (6,4) and (5,7) can be created.
在第一个测试用例中,可以组建团队 (3,4)。
在第二个测试用例中,无法组建任何团队,因为仅有 2 名员工,且其中一人是另一人的上级。
在第三个测试用例中,可以组建团队 (2,3)。
在第四个测试用例中,可以组建团队 (2,4)、(3,5) 和 (6,7)。
在第五个测试用例中,可以组建团队 (2,3)、(6,4) 和 (5,7)。
输入解题思路,AI测评打分。不知道怎么写?