https://cses.fi/problemset/task/1674
Can anyone help me code solution for this question?
My Approach (Based on the fact that this problem is placed under the Trees section. Hope it helps).
For instance,
5
1 1 2 3
// Parent[2] = 1
// Parent[3] = 1
// Parent[4] = 2
// Parent[5] = 3
This input represents the following tree (rooted at 1).
Now, run a DFS, where at each node we perform the following. Each node returns the value (number_of_subOrdinates + 1)
:
Now, the above image represents the same.
We can store all these values in an Array while performing DFS.
Finally, we iterate over the array and output these values.
Thanks guys