题解 | 上司的舞会

上司的舞会

https://www.nowcoder.com/practice/45c6d97dfd1044769aed5d9d3f139be1

import collections
import sys


# for line in sys.stdin:
#     a = line.split()
#     print(int(a[0]) + int(a[1]))

n = int(sys.stdin.readline())

p_list = []
p_line = sys.stdin.readline()
p_str_list = p_line.split()
for p_str in p_str_list:
    p = int(p_str)
    p_list.append(p)

if len(p_list) == 0:
    print(0)
else:
    level = 0
    p_map = collections.defaultdict(list)
    for i, p in enumerate(p_list):
        p_map[p].append(i+1)

    nodes = p_map[-1]
    while nodes:
        level += 1
        new_nodes = []
        for node in nodes:
            new_nodes.extend(p_map[node])

        nodes = new_nodes

    print(level)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务