#P2971. [USACO10HOL] Cow Politics G

[USACO10HOL] Cow Politics G

Description

Farmer John's cows live on nn different pastures, numbered 1n1 \sim n.

There are exactly n1n-1 undirected roads of unit length connecting these pastures, and from any pasture you can reach all others. In other words, the pastures and roads form a tree. The input specifies, for each pasture ii, its parent pip_i. The root has pi=0p_i = 0, meaning it has no parent.

There are kk political parties numbered 1k1 \sim k. Each cow joins exactly one party; specifically, cow ii (at pasture ii) belongs to party aia_i. Each party has at least two cows. Every party wants to know its "range", defined as the distance (along the undirected roads) between the farthest pair of cows within that party.

Input Format

The first line contains two integers n,kn, k.

Lines 2n+12 \sim n+1: line i+1i+1 contains two integers ai,pia_i, p_i.

Output Format

Output kk lines. The ii-th line contains a single integer, the range of party ii.

6 2 
1 3 
2 1 
1 0 
2 1 
2 1 
1 5 

3 
2 

Hint

Constraints: $2 \le n \le 2 \times 10^5, 1 \le k \le \frac n2, 0 \le p_i \le n, 1 \le a_i \le k$.

Translated by ChatGPT 5