#P3605. Cow Subordinates Promotion Analysis
Cow Subordinates Promotion Analysis
Cow Subordinates Promotion Analysis
Cows have once again tried to start a company, only to repeat history – cows are terrible managers! The company is organized as a tree: cows are numbered from 1 to n with cow 1 as the president (the root of the tree), and every other cow has a unique direct supervisor. For every cow i, its subordinate j is any cow in the subtree rooted at i (that is, any descendant of i). Each cow i has a unique ability index \( p_i \), which measures how good she is at her job.
An issue arises when a supervisor has a lower ability than some of her subordinates. Your task is, for every cow i, to count the number of its subordinates j with \( p_j > p_i \).
Note: All formulas are written in \( \LaTeX \) format.
inputFormat
The input begins with a line containing an integer \( n \) (the number of cows). The second line contains \( n \) space-separated integers \( p_1, p_2, \ldots, p_n \) representing the ability indices of the cows. The third line contains \( n-1 \) space-separated integers; the i-th integer (for \( i = 1, 2, \ldots, n-1 \)) denotes the supervisor (boss) of cow \( i+1 \). It is guaranteed that these relationships form a tree, with cow 1 as the root.
outputFormat
Output a single line containing \( n \) space-separated integers. The \( i \)-th integer represents the number of subordinate cows of cow \( i \) whose ability index is greater than \( p_i \).
sample
5
3 2 5 1 4
1 1 2 2
2 1 0 0 0