Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for New Cities by eugene100372
def subnetworks(net, crushes):
s1=set(crushes)
d=dict()
for nod1,nod2 in net:
if nod1 not in s1: d[nod1]=d.get(nod1,set())|(set(nod2)-s1)
if nod2 not in s1: d[nod2]=d.get(nod2,set())|(set(nod1)-s1)
res=0
s2=set(d.keys())
while s2:
s3={s2.pop()}
while s3:
s4=set()
for el in s3:
s4|=(d[el]&s2)
s3=s4-s3
s2-=s3
res+=1
return res
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert subnetworks([
['A', 'B'],
['B', 'C'],
['C', 'D']
], ['B']) == 2, "First"
assert subnetworks([
['A', 'B'],
['A', 'C'],
['A', 'D'],
['D', 'F']
], ['A']) == 3, "Second"
assert subnetworks([
['A', 'B'],
['B', 'C'],
['C', 'D']
], ['C', 'D']) == 1, "Third"
print('Done! Check button is waiting for you!')
Nov. 20, 2019