携程笔试求解
第三题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)
上海得物信息集团有限公司公司福利 1208人发布
查看15道真题和解析