#P10846. [EGOI 2024] Team Coding / 团队编程
[EGOI 2024] Team Coding / 团队编程
Background
Day 1 Problem C.
The statement is translated from EGOI2024 teamcoding. The translation comes from ChatGPT and has been proofread manually. If there are any mistakes, please contact rui_er.
Problem Description
The Eindhoven Gigantic Open-Source Institute (EGOI) company has a very strict hierarchy. Besides the CEO Anneke, each of the other employees has exactly one direct boss, and there are no cycles in the hierarchy. You can view the company hierarchy as a tree rooted at Anneke. Since this is a diverse company, employees use different programming languages, but each employee has one preferred programming language.
Anneke has prepared a large new project for the company, and she wants to invest as many resources as possible. To decide which team will take charge of this project, she does the following:
-
Choose one person to lead the team. This also determines the programming language used for the project. Every employee in the subtree under the team leader whose preferred programming language is the same as the team leader’s will participate in the project.
-
Increase the number of employees participating in the project by moving employees whose preferred programming language is the same as the team leader’s into her team.
To maximize the number of participating employees, she may perform the following swap operation multiple times:
- She chooses two employees:
- One employee who is currently in the team leader’s subtree and whose preferred programming language is different from the team leader’s.
- One employee who is currently not in this subtree and whose preferred programming language is the same as the team leader’s. In addition, this employee must be at the same level as the other chosen employee, meaning they have the same number of bosses on the chain reporting up to Anneke. If you view the company hierarchy as a tree, then these two employees are on the same depth.
- These two employees (and only them) swap their positions in the company hierarchy. Note that the employees who report to these two employees stay the same; only who they report to is changed. In the example below, when choosing employee as the team leader, we can swap employees and , but we cannot swap employees and .
- She chooses two employees:

Find the maximum number of employees that can participate in the new project, and the minimum number of swaps needed to achieve it.
Input Format
The first line contains two integers, and , representing the number of employees in EGOI and the number of programming languages that employees may use.
EGOI employees are numbered from to , and CEO Anneke has number . The next line contains integers , where , indicating the preferred programming language of employee .
The next lines describe the company structure. Line contains an integer , where , indicating the direct boss of employee . Note that ranges from to (inclusive), because CEO Anneke has no boss.
Output Format
Output one line containing two integers, and , representing the maximum number of employees that can participate in the project (including the team leader) after any number of swaps, and the minimum number of swaps needed to achieve this maximum.
5 3
0 1 2 2 1
0
1
2
3
2 0
4 2
0 1 0 0
0
0
1
3 0
9 3
0 0 2 1 2 0 2 1 2
4
8
1
0
4
1
0
7
4 2
8 3
0 2 1 2 2 1 1 1
6
3
0
6
3
0
3
3 2
Hint
Sample Explanation
In the first two samples, the company structure is as shown below, where the patterns encode the programming languages (0 = “striped”, 1 = “dotted”, 2 = “solid”):

In sample 1, we can choose employee as the team leader. Employee likes the same programming language, and there is no possible swap to improve this. In sample 2, the whole company has employees who like language , which is also the language Anneke likes, so choosing Anneke as the team leader forms a team of size and requires no swaps.

In sample 3, we choose employee as the team leader, and then we can swap the teams of employees and and of employees and , obtaining a total of employees who like the same language as , namely language (“solid”). In sample 4, choosing employee as the team leader and swapping employees and as well as employees and achieves the maximum score. Note that we cannot swap employees and before choosing the team leader to get score , because we must determine the team leader first.
Constraints
For all testdata, , .
- Subtask 1 ( points): Employee ’s boss is .
- Subtask 2 ( points): .
- Subtask 3 ( points): For each programming language, at most employees prefer it.
- Subtask 4 ( points): .
- Subtask 5 ( points): No additional constraints.
Note: Some test points are placed into multiple subtasks in EGOI. To save judging resources and the workload of organizing the data, these test points are placed in the subtask with the smallest index among all subtasks that contain them. This may cause a solution to get a higher score than expected, but still not pass the problem.
Translated by ChatGPT 5