absy's blog

By absy, history, 3 years ago, In English

913B - Новогодняя елка

119557653

n= int(input())
ls=[]
for i in range(n):
    ls.append([])
 
for i in range(2,n+1):
    j=int(input())
    ls[j-1].append(i)
    
for i in ls:
    for j in i:
        if ls[j-1]:
            i.remove(j)
 
 
ls = list(filter(([]).__ne__, ls))
 
if len(min(ls))<3:
    print('No')
else:
    print('Yes')

Basically, I create a nested list where I store each node's child vertexes in different elements(eg: indexes of the child vertexes of the root node are in the 0th element/ the first element) of the list. Then, I remove the index of the child vertex (if that child vertex has further children) from the element associated with the parent vertex.

For eg: if 1 had 3 children, 2 3 and 4, and if 2 had further children, then we remove 2 from the element associated with the root, i.e. the 0th element in the list.

so before change -> [[2,3,4],[5,6,7]] after change -> [[3,4],[5,6,7]]

I'd appreciate the help especially since I'm a noob at coding :)

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it