Codeforces Round 434 (Div. 1, based on Technocup 2018 Elimination Round 1)
E
Arkady and a Nobody-men
Codeforces
2700
原题
← D→
翻译中…

Arkady words in a large company. There are $*n*$ employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.

翻译中…

Each employee has an integer rank. The CEO has rank equal to $1$, each other employee has rank equal to the rank of his immediate manager plus $1$.

翻译中…

Arkady has a good post in the company, however, he feels that he is nobody in the company's structure, and there are a lot of people who can replace him. He introduced the value of replaceability. Consider an employee $*a*$ and an employee $*b*$, the latter being manager of $*a*$ (not necessarily immediate). Then the replaceability $*r*(*a*, *b*)$ of $*a*$ with respect to $*b*$ is the number of subordinates (not necessarily immediate) of the manager $*b*$, whose rank is not greater than the rank of $*a*$. Apart from replaceability, Arkady introduced the value of negligibility. The negligibility $*z*_{*a*}$ of employee $*a*$ equals the sum of his replaceabilities with respect to all his managers, i.e. image, where the sum is taken over all his managers $*b*$.

翻译中…

Arkady is interested not only in negligibility of himself, but also in negligibility of all employees in the company. Find the negligibility of each employee for Arkady.

翻译中…

Input

翻译中…

The first line contains single integer $*n*$ ($1 ≤ *n* ≤ 5·10^{5}$) — the number of employees in the company.

翻译中…

The second line contains $*n*$ integers $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ ($0 ≤ *p*_{*i*} ≤ *n*$), where $*p*_{*i*} = 0$ if the $*i*$-th employee is the CEO, otherwise $*p*_{*i*}$ equals the id of the immediate manager of the employee with id $*i*$. The employees are numbered from $1$ to $*n*$. It is guaranteed that there is exactly one $0$ among these values, and also that the CEO is a manager (not necessarily immediate) for all the other employees.

翻译中…

Output

翻译中…

Print $*n*$ integers — the negligibilities of all employees in the order of their ids: $*z*_{1}, *z*_{2}, ..., *z*_{*n*}$.

翻译中…

Note

翻译中…

Consider the first example:

翻译中…
  • The CEO has no managers, thus $*z*_{1} = 0$.
  • $*r*(2, 1) = 2$ (employees $2$ and $4$ suit the conditions, employee $3$ has too large rank). Thus $*z*_{2} = *r*(2, 1) = 2$.
  • Similarly, $*z*_{4} = *r*(4, 1) = 2$.
  • $*r*(3, 2) = 1$ (employee $3$ is a subordinate of $2$ and has suitable rank). $*r*(3, 1) = 3$ (employees $2$, $3$, $4$ suit the conditions). Thus $*z*_{3} = *r*(3, 2) + *r*(3, 1) = 4$.
翻译中…

样例

Input #1
4
0 1 2 1
Output #1
0 2 4 2
InputCopy OutputCopy
Input #2
5
2 3 4 5 0
Output #2
10 6 3 1 0
InputCopy OutputCopy
Input #3
5
0 1 1 1 3
Output #3
0 3 3 3 5
InputCopy OutputCopy
题目信息
题号
860E
时间限制
2 s
内存限制
256 MB
标签
数据结构
DFS 及类似
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。