最近在尝试实现google kickstater 2019编程题的解决方案,按照分析解释尝试实现Round E的Cherries Mesh。这是问题和分析的链接。 https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050edb/0000000000170721
这是我实现的代码:
t = int(input())
for k in range(1,t+1):
n, q = map(int,input().split())
se = list()
for _ in range(q):
a,b = map(int,input().split())
se.append((a,b))
l = [{x} for x in range(1,n+1)]
#print(se)
for s in se:
i = 0
while ({s[0]}.isdisjoint(l[i])):
i += 1
j = 0
while ({s[1]}.isdisjoint(l[j])):
j += 1
if i!=j:
l[i].update(l[j])
l.pop(j)
#print(l)
count = q+2*(len(l)-1)
print('Case #',k,': ',count,sep='')
这通过了示例用例,但没有通过测试用例。据我所知,这应该是正确的。难道我做错了什么?