Official

C - Ameba Editorial by en_translator


The sought answer for an amoeba is the answer for its parent plus \(1\). Thus, one can solve the problem in \(O(N)\) by recording whenever a new amoeba is born.

Sample code (Python)

N=int(input())
A=list(map(int,input().split()))
ans=[0]*(2*N+1)
for i,a in enumerate(A):
  ans[2*i+1]=ans[a-1]+1
  ans[2*i+2]=ans[a-1]+1

print(*ans,sep="\n")

posted:
last update: