PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000001. Đếm nhân viên cấp dưới (Subordinates)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm nhân viên cấp dưới (Subordinates)
Subordinates
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
A company employs people numbered from to . Employee is the director. Every other employee has exactly one immediate manager, and the management hierarchy forms a rooted tree.
An employee is a subordinate of if following immediate managers one or more times leads to . Employee is not their own subordinate.
Determine the number of direct and indirect subordinates of every employee.
Input
- The first line contains .
- The second line contains integers ; is the immediate manager of employee .
Output
Print integers in employee order . The -th integer is the number of subordinates of employee .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; .
Examples
Input
5
1 1 2 3
Output
4 1 1 0 0
Explanation
Employee supervises the other four employees. Employee supervises , and employee supervises . Employees and supervise nobody.