#P2971. Political Party Range

    ID: 16229 Type: Default 1000ms 256MiB

Political Party Range

Political Party Range

Farmer John's cows live on N pastures connected by N-1 unit-length bidirectional paths forming a tree. Each pasture i has a parent P_i (with P_i = 0 indicating that the pasture is the root) and a cow that belongs to a political party A_i. There are K political parties numbered from 1 to K, and each party has at least two cows. The range of a party is defined as the largest distance between any two cows in that party, where the distance is measured as the number of paths between their pastures.

Your task is to determine the range for each political party.

Note: If a formula is used, it must be in \(\LaTeX\) format. For example, the distance between two nodes u and v can be computed as \(\text{dist}(u,v) = \text{depth}(u) + \text{depth}(v) - 2\cdot\text{depth}(\mathtt{LCA}(u,v))\).

inputFormat

The input consists of three parts:

  1. The first line contains two integers N and K — the number of pastures and the number of political parties respectively.
  2. The second line contains N integers: P1, P2, \ldots, PN, where Pi = 0 indicates that pasture i is the root.
  3. The third line contains N integers: A1, A2, \ldots, AN, where Ai is the political party of the cow on pasture i.

It is guaranteed that each political party has at least two cows.

outputFormat

Output K lines. The i-th line should contain a single integer representing the range of political party i.

sample

6 2
0 1 1 3 1 4
1 2 1 1 2 1
3

2

</p>