携程笔试求解

第三题rgb只过了12.5,请大佬们帮忙看下有啥问题!感谢!!!
from collections import Counter,defaultdict
n = int(input())
s = input()
dic = defaultdict(lambda : [])
for i in range(n-1):
    a,b = list(map(int,input().split()))
    dic[a].append(b)
cnt = Counter(s)
res = 0

def dfs(x,tmp_dic):
    global res
    if x not in dic:
        tmp_dic[s[x-1]] = tmp_dic.get(s[x-1],0) + 1
        return tmp_dic
    for i in dic[x]:
        tag = dfs(i,{})
        for z in tag.keys():
            tmp_dic[z] = tmp_dic.get(z,0) + tag[z]
    tmp_dic[s[x-1]] = tmp_dic.get(s[x-1],0) + 1
    if len(tmp_dic)==3 and (cnt['r'] - tmp_dic['r']>0 and cnt['g'] - tmp_dic['g']>0 and cnt['b'] - tmp_dic['b']>0):
        res += 1
    return tmp_dic

dfs(1,{})
print(res)


#携程笔试#
全部评论
终于看到了个python的了😂
点赞 回复 分享
发布于 2022-08-30 23:44 浙江
无向图
1 回复 分享
发布于 2022-08-30 21:10 广西
口胡思路 https://www.nowcoder.com/discuss/1033903
点赞 回复 分享
发布于 2022-08-30 21:36 广西
有第四题的代码吗
点赞 回复 分享
发布于 2022-08-30 21:16 广东

相关推荐

07-10 11:08
门头沟学院 Java
投递京东等公司9个岗位
点赞 评论 收藏
分享
06-07 17:17
嘉兴学院 教师
单单人旁的佳:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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