#FT0000175. Đếm cấp dưới giỏi hơn (Promotion Counting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm cấp dưới giỏi hơn (Promotion Counting)
Promotion Counting
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
A company employs people numbered through . Employee is the president; every other employee has exactly one direct manager . These relations form a tree rooted at . Employee has a proficiency rating , and all ratings are pairwise distinct. A subordinate of means any proper descendant of , including both direct and indirect reports; itself is never counted. For every employee , count the subordinates whose ratings satisfy the strict inequality .
Input
The first line contains . The following lines give the ratings (one per line). The next lines give for in this order. The management relation is a tree rooted at employee .
Output
Print exactly lines. The -th line contains the number of proper descendants with a strictly higher rating than employee . Each leaf contributes zero.
Subtasks
- Subtask 1 (20 points): ; and all proficiency ratings are distinct; root 1, exactly one parent for each , with , and these edges form a rooted tree.
- Subtask 2 (30 points): ; and all proficiency ratings are distinct; root 1, exactly one parent for each , with , and these edges form a rooted tree.
- Subtask 3 (50 points): ; and all proficiency ratings are distinct; root 1, exactly one parent for each , with , and these edges form a rooted tree.
Examples
Example 1
Input
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Output
2
0
1
0
0
Explanation
- Employee 1: proficiency 804289384; descendants [2, 3, 4, 5]; higher-proficiency descendants [2, 5]. Output 2.
- Employee 2: proficiency 846930887; descendants [4]; higher-proficiency descendants none. Output 0.
- Employee 3: proficiency 681692778; descendants [5]; higher-proficiency descendants [5]. Output 1.
- Employee 4: proficiency 714636916; descendants none; higher-proficiency descendants none. Output 0.
- Employee 5: proficiency 957747794; descendants none; higher-proficiency descendants none. Output 0.
Example 2
Input
4
10
30
20
40
1
1
2
Output
3
1
0
0
Explanation
- Employee 1: proficiency 10; descendants [2, 3, 4]; higher-proficiency descendants [2, 3, 4]. Output 3.
- Employee 2: proficiency 30; descendants [4]; higher-proficiency descendants [4]. Output 1.
- Employee 3: proficiency 20; descendants none; higher-proficiency descendants none. Output 0.
- Employee 4: proficiency 40; descendants none; higher-proficiency descendants none. Output 0.