Team Coding

시간 제한4초메모리 제한1024 MB

요약
색이 칠해진 정점으로 이루어진 루트 트리에서 팀장을 정한 뒤 같은 레벨의 정점을 맞바꿔 팀장의 부분 트리 안에 같은 색 정점 수를 최대로 만들고, 그때 필요한 최소 교환 횟수를 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 그리디, 구현
정답자
아직 제출이 없습니다

문제

The company Eindhoven Gigantic Open-Source Institute (EGOI) is structured in a very hierarchical way. Except for the CEO Anneke, each of the other N−1N-1 employees in the company has a unique boss that they report to, and there are no cycles in the hierarchy. You can think of the company hierarchy as a tree rooted in the vertex corresponding to Anneke. As this is a diverse company, the employees code in KK different programming languages, but every employee has exactly one preferred programming language.

Anneke has a big new project for a team in her company to work on. She wants to put as many resources as possible into this project. To decide the team who will work on this, she does the following:

  1. Pick a person to lead the team. This will also define the programming language the project is coded in. Every employee who is in the subtree below the team lead and prefers the same programming language will work on the problem.

  2. Increase the number of employees who work on the project, by switching employees who prefer the same programming language as the team lead into her team. To maximize the number of employees who work on the project, she can perform the following switching operation any number of times:

    1. She picks two employees:

      1. One employee who is currently in the subtree of the team lead and does not prefer the same programming language as the team lead.
      2. One employee who is not in this subtree at the moment and prefers the same programming language as the team lead. Additionally, this employee needs to be on the same level as the other chosen employee; that is, they need to have the same number of higher-ups in the report chain to Anneke. If you imagine the company hierarchy as a tree, then the two employees are on the same level of the tree.
    2. Those two employees (and only them -- not any other employees) switch positions in the company hierarchy. Note that employees reporting to the two affected employees stay in place and just change whom they report to. In the example below, with employee 44 having been chosen as the team lead, we can swap employees 33 and 22 but not employees 11 and 88.

Find the maximum number of employees working on the new project you can reach and the minimum number of switching operations needed to achieve this.

입력

The first line of the input contains two integers, NN and KK, the number of employees of EGOI and the number of programming languages the employees might use.

The employees of EGOI are numbered from 00 to N−1N-1, and Anneke the CEO has number 00. The next line contains NN integers ℓ_i\ell\_i with 0≤ℓ_i\<K0\le \ell\_i\<K, the preferred programming languages of the employees.

The next N−1N-1 lines contain the company structure. The iith line contains an integer b_ib\_i with 0≤b_i\<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 a single line with two integers, PP and SS, the maximum number of employees (including the team lead) working on the new project you can reach with any number of switches and the minimum number of switches needed to reach this.

제한

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

힌트

In the first two samples, the company structure looks as follows, where the pattern encodes the programming language (0 = "striped", 1 = "dotted", 2 = "plain"):

In sample 1, we can choose employee 11 as the team lead with employee 44 preferring the same programming language and there are no possible switches to improve this. In sample 2, the full company has 33 employees preferring language 00 which is also Anneke's preferred language, so choosing Anneke as the team lead gives a team of size 33 with no switches needed.

In sample 3, we choose employee 44 as the team lead and then we can have employees 11&88 and 22&33 switch teams to get a total of 44 employees preferring the same language as 44, namely language 22 (yellow/plain). In sample 4, the maximum score can be obtained by choosing employee 66 as the team lead and switching employees 44&77 and 11&55. Note that we cannot switch employees 66&33 before choosing the team lead to get a score of 44 because we have to fix the team lead first.

예제4

  1. 예제 1

    입력
    5 3
    0 1 2 2 1
    0
    1
    2
    3
    
    예상 출력
    2 0
    
  2. 예제 2

    입력
    4 2
    0 1 0 0
    0
    0
    1
    
    예상 출력
    3 0
    
  3. 예제 3

    입력
    9 3
    0 0 2 1 2 0 2 1 2
    4
    8
    1
    0
    4
    1
    0
    7
    
    예상 출력
    4 2
    
  4. 예제 4

    입력
    8 3
    0 2 1 2 2 1 1 1
    6
    3
    0
    6
    3
    0
    3
    
    예상 출력
    3 2