Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Power Supply by Oleg_Domokeev
def power_supply(network, power_plants):
not_connected = set().union(*network)
for pp, dist in power_plants.items():
visited = set()
stack = [(pp, 0)]
while stack:
node, step = stack.pop()
visited.add(node)
if step < dist:
for N1, N2 in network:
if node == N1 and N2 not in visited:
stack.append((N2, step+1))
if node == N2 and N1 not in visited:
stack.append((N1, step+1))
not_connected -= visited
if not not_connected:
break
return not_connected
March 12, 2020