Bureaucracy
Time limit1sMemory limit64 MB
Simulate repeatedly sending a task down the smallest-numbered child on each root-to-leaf path, paying 1,2,3,... coins up the chain, and deleting the leaf.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Simulation, Brute force
- Solved
- No attempts yet
Problem
Mirko is now the CEO of a huge corporation. The corporation has people, numbered to , and Mirko is number . Everyone except Mirko has exactly one boss, and we call that person an assistant of the boss. A boss can have several assistants and still reports to their own boss. Mirko is the exception. He sits at the top of the pyramid, so he has no boss, only assistants.
When the investors give Mirko a task, he passes it to his assistant with the smallest number. That assistant passes it to their own assistant with the smallest number, and this repeats until the task reaches somebody with no assistants. That person does the work.
This is where the real problem starts. The person who did the task gets coin, that person's boss gets coins, the boss of that boss gets coins, and so on up to Mirko, who gets as many coins as there are people in the chain. After the payout, the employee who actually did the work decides the system is unfair and quits.
The next task is handled with one person fewer in the corporation, so the payouts can be smaller, but the work must go on. Tasks keep piling up, so the whole procedure (assigning a task, doing it, splitting the coins, and losing the worker) repeats until Mirko is alone and does his first and last task himself.
Mirko will have collected a fortune by then, and he also wants to know how much every employee earned.
Input
The first line contains the integer (), the number of employees including Mirko.
The second line contains integers (), where is the number of the boss of employee .
Output
Print one line with numbers separated by single spaces. The th number is the total number of coins employee earns.
Hint
Follow the sample with . Mirko gives the first task to employee , who gives it to employee , who does the work. Employee gets coin, employee gets coins, and employee (Mirko) gets coins. Employee then quits.
Mirko gives the second task to employee again. Employee is gone, so employee passes it to employee , who passes it to employee , who does the work. Employee gets coin, employee gets coins, employee gets coins, and employee gets coins. Employee then quits.
The procedure runs for tasks in total. Mirko ends up with coins, employee with , employee with , and employees and with coin each.