LearnPy Hub
← Arena
IntermediateRating 1300

Size of Every Subtree

A tree has n nodes rooted at node 1. For nodes 2 … n you are given the parent. Print the size of every node's subtree (the node plus everything below it), for nodes 1 … n.

Input

n, then n − 1 parents (of nodes 2 … n).

Output

n integers.

Sample 1

Input
5
1 1 2 2
Output
5 3 1 1 1

Sample 2

Input
3
1 2
Output
3 2 1
solution.py
Output appears here
Size of Every Subtree · Python Arena problem · LearnPy Hub