题解 | #小红的树#

小红的树

https://www.nowcoder.com/practice/66ab364d3fba487eb39bd3460fd484c0

import sys
from collections import defaultdict

# 后序遍历(所谓树形DP)
n = int(input())
sons = defaultdict(list)
ps = list(map(int, input().split()))
for i in range(len(ps)):
    sons[ps[i]].append(i+2)

color = ' ' + input()
dp = [0] * (n+1)

def postorder(id):
    for s in sons[id]:
        postorder(s)
    if color[id] == 'R':
        dp[id] += 1
    for s in sons[id]:
        dp[id] += dp[s]

postorder(1)
q = int(input())
for i in range(q):
    print(dp[int(input())])

#笔试速成法#
全部评论
只会用Python解,求golang版代码
点赞 回复 分享
发布于 2023-03-22 15:53 广东

相关推荐

zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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