#P2971. [USACO10HOL] Cow Politics G
[USACO10HOL] Cow Politics G
Description
Farmer John's cows live on different pastures, numbered .
There are exactly 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 , its parent . The root has , meaning it has no parent.
There are political parties numbered . Each cow joins exactly one party; specifically, cow (at pasture ) belongs to party . 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 .
Lines : line contains two integers .
Output Format
Output lines. The -th line contains a single integer, the range of party .
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
京公网安备 11011102002149号