Team Coding
Time limit4sMemory limit1024 MB
Pick a team lead in a rooted tree so that, after swaps between same-depth employees, the most people share the lead's language, and count the minimum swaps.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Binary search, Sorting
- Solved
- No attempts yet
Problem
EGOI (Eindhoven Gigantic Open-Source Institute) has a very hierarchical structure. Except for the CEO Anneke, each of the other employees has exactly one boss they report to, and the hierarchy has no cycles. The company hierarchy is a tree rooted at Anneke. The company is diverse: employees code in one of programming languages, and every employee has exactly one preferred language.
Anneke has a big new project for a team to work on, and she wants to put as many people into it as possible. She chooses the team as follows.
- Pick a team lead. The language the lead prefers becomes the language the project is coded in. Every employee in the lead's subtree who prefers the same language joins the project.
- Move employees who prefer the lead's language into the team to increase the number of people on the project. She can perform the following switching operation any number of times.
- She picks two employees.
- One employee who is in the lead's subtree and does not prefer the lead's language.
- One employee who is not in that subtree and prefers the lead's language. This employee must be on the same level as the other chosen employee, meaning they have the same number of bosses in the report chain up to Anneke. In the tree, the two employees are at the same depth.
- Only those two employees switch positions in the hierarchy. Nobody else moves. Employees who report to either of the two stay where they are and only change whom they report to. In the example below, with employee 4 as the team lead, employees 3 and 2 can be swapped, but employees 1 and 8 cannot.
- She picks two employees.

Find the maximum number of employees working on the project, including the team lead, that can be reached with any number of switches, and the minimum number of switches needed to reach it.
Input
The first line contains two integers and , the number of employees at EGOI and the number of programming languages the employees might use. The employees are numbered from to , and Anneke the CEO is number . The next line contains integers with , the preferred programming language of each employee. The next lines describe the company structure. The th line contains an integer with , the direct boss of the th employee. Note that goes from to , as Anneke, the CEO, does not have a boss.
Output
Output a single line with two integers and . is the maximum number of employees (including the team lead) working on the project that can be reached with any number of switches. is the minimum number of switches needed to reach that number.
Constraints
- .
- .
Hints
The company structure in the first two examples is shown below. The pattern encodes the programming language (0 is striped, 1 is dotted, 2 is plain).

In the first example, we can choose employee 1 as the team lead, with employee 4 preferring the same language, and no switch can improve this. In the second example, the full company has 3 employees preferring language 0, which is also Anneke's preferred language. Choosing Anneke as the team lead gives a team of 3 with no switches needed.

In the third example, we choose employee 4 as the team lead. Then employees 1 and 8, and employees 2 and 3, switch places, giving 4 employees who prefer the same language as employee 4, namely language 2 (yellow, plain). In the fourth example, the maximum is reached by choosing employee 6 as the team lead and switching employees 4 and 7, and employees 1 and 5. We cannot switch employees 6 and 3 before choosing the team lead to get 4 employees, because the team lead has to be fixed first.