求助:美团第五道为啥只能c20%
3
3 3 12
1 3
2 3
我的思路:
data=[3,3,12]
color=["white","white","white"]
// 使用字典保存树,存储所有的联通对的下标(减1变为index)
Tree={
0:{2},
1:{2},
2:{0,1}
}
count=0
for node,nextnodes in Tree.items():
for nextnode in nextnodes:
if color[node]=="white" and color[nextnode]=="white":
v1,v2=data[node],data[nextnode]
multi=v1*v2
if int(math.sqrt(multi))**2 == multi:
count+=2
color[node],color[nextnode]="red","red"
return count
我感觉思路没问题,测试用例也过了
但只能c 20%,不知道哪里错了,求大佬帮忙指教
#美团##美团信息集散地##笔试##算法#
3 3 12
1 3
2 3
我的思路:
data=[3,3,12]
color=["white","white","white"]
// 使用字典保存树,存储所有的联通对的下标(减1变为index)
Tree={
0:{2},
1:{2},
2:{0,1}
}
count=0
for node,nextnodes in Tree.items():
for nextnode in nextnodes:
if color[node]=="white" and color[nextnode]=="white":
v1,v2=data[node],data[nextnode]
multi=v1*v2
if int(math.sqrt(multi))**2 == multi:
count+=2
color[node],color[nextnode]="red","red"
return count
我感觉思路没问题,测试用例也过了
但只能c 20%,不知道哪里错了,求大佬帮忙指教
#美团##美团信息集散地##笔试##算法#
全部评论
前面的优先选择不一定最优,完全的回溯法又超时,我也是20%,不知道咋做
我用贪心有一个case没过,好像要用树状dp
能在本地编译器写吗?
4
1 1 1 1
1 2
2 3
1 4
试试
相关推荐

点赞 评论 收藏
分享

点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享