Under construction

Still being built. Things may change

Team Coding

Time limit4sMemory limit1024 MB

Summary
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 N−1N-1 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 KK 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.

  1. 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.
  2. 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.
    1. She picks two employees.
      1. One employee who is in the lead's subtree and does not prefer the lead's language.
      2. 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.
    2. 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.

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 NN and KK, the number of employees at EGOI and the number of programming languages the employees might use. The employees are numbered from 00 to N−1N-1, and Anneke the CEO is number 00. The next line contains NN integers ℓi\ell_i with 0≤ℓi<K0 \le \ell_i < K, the preferred programming language of each employee. The next N−1N-1 lines describe the company structure. The iith line contains an integer bib_i with 0≤bi<N0 \le b_i < N, the direct boss of the iith employee. Note that ii goes from 11 to NN, as Anneke, the CEO, does not have a boss.

Output

Output a single line with two integers PP and SS. PP is the maximum number of employees (including the team lead) working on the project that can be reached with any number of switches. SS is the minimum number of switches needed to reach that number.

Constraints

  • 1≤N≤1051 \le N \le 10^5.
  • 1≤K≤N1 \le K \le N.

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.

Examples4

  1. Example 1

    Input
    5 3
    0 1 2 2 1
    0
    1
    2
    3
    
    Expected output
    2 0
    
  2. Example 2

    Input
    4 2
    0 1 0 0
    0
    0
    1
    
    Expected output
    3 0
    
  3. Example 3

    Input
    9 3
    0 0 2 1 2 0 2 1 2
    4
    8
    1
    0
    4
    1
    0
    7
    
    Expected output
    4 2
    
  4. Example 4

    Input
    8 3
    0 2 1 2 2 1 1 1
    6
    3
    0
    6
    3
    0
    3
    
    Expected output
    3 2