Created
July 12, 2017 02:04
-
-
Save IKKO-Ohta/ca1e62f5ae5116c084e93327d0b3ec52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = int(input()) | |
boss = {i:[] for i in range(N)} | |
S = [i for i in range(N)] | |
for worker in range(1,N): | |
bs = int(input()) - 1 | |
boss[bs].append(worker) | |
for i in reversed(range(N)): | |
MIN,MAX = 10000000,-1 | |
if not boss[i]: S[i] = 1 | |
elif len(boss[i]) == 1: S[i] = S[boss[i][0]] * 2 + 1 | |
else: | |
for worker in boss[i]: | |
if S[worker] < MIN: MIN = S[worker] | |
if S[worker] > MAX: MAX = S[worker] | |
S[i] = MAX + MIN + 1 | |
print(S[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment