#20951: 求大神優化(8~13一直TLEQQ)


youngyoung (06 蘇昱揚 306)


nodes = eval(input())
leaf = []
TreeList = [[i + 1] for i in range(nodes)] #set node
for i in range(nodes): #set NumOfSons, Sons
  lst = input()
  NumOfSons = eval(lst.split()[0])
  Sons = [eval(j) for j in lst.split()[1:]]
  TreeList[i].append(NumOfSons)
  TreeList[i].append(tuple(Sons))
  if NumOfSons == 0:
    leaf.append(i)
for i in range(nodes): #set Father
  if TreeList[i][1] != 0:
    for j in range(TreeList[i][1]):
      TreeList[TreeList[i][2][j] - 1].append(i + 1)
for i in range(nodes): #find root
  if len(TreeList[i]) == 3:
    root = i + 1
    TreeList[i].append(0)
    break
for i in range(nodes):
  TreeList[i].append(0)
for i in leaf: #set possible hight
  hight = 0
  j = i
  while j != root - 1:
    if hight > TreeList[j][4]:
      TreeList[j][4] = hight
    hight += 1
    j = TreeList[j][3] - 1
  if hight > TreeList[root - 1][4]:
    TreeList[root - 1][4] = hight
TotalHight = 0
for i in range(nodes):
  TotalHight += TreeList[i][4]
print(root)
print(TotalHight)
#20964: Re:求大神優化(8~13一直TLEQQ)


asnewchien@gmail.com (david)


#20978: Re:求大神優化(8~13一直TLEQQ)


youngyoung (06 蘇昱揚 306)