405958: GYM102191 I Project Presentation

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

I. Project Presentationtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a tree that represents a hierarchy in a company, where the parent of node u is their direct manager.

Each employee is assigned a project, and multiple employees can be assigned to the same project. When it is time for the presentation of the ith project, all employees u that are assigned that project and their direct and indirect managers must attend the presentation (u and their manager and their manager's manager and so on until the CEO).

Find for each project the number of people attending its presentation.

Input

The first line of input is n and m (1 ≤ m ≤ n ≤ 106), the number of employees and the number of projects, respectively.

The second line of input contains n integers ai (1 ≤ ai ≤ m), where ai is the project assigned to the ith employee. It is guaranteed that each project has at least one employee assigned to it.

The third line of input contains n integers pi (0 ≤ pi ≤ n), where pi is the direct manager of the ith employee. If pi = 0, then the ith employee is the CEO and does not have a manager. It is guaranteed that there is only one CEO, and this CEO is a direct or indirect manager of all other employees.

Output

Output m integers, where the ith integer is the number of people attending the presentation of the ith project.

ExampleInput
6 4
1 2 4 3 2 4
0 1 1 3 3 2
Output
1 4 3 4 

加入题单

算法标签: