Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
dfs solution in Clear category for How to Find Friends by roenke
import collections
def dfs(elem, g, visited):
visited[elem] = 1
for u in g[elem]:
if visited[u] == 0:
dfs(u, g, visited)
def check_connection(network, first, second):
g = collections.defaultdict(list)
visited = collections.defaultdict(lambda: 0)
for elem in network:
v, u = elem.split('-')
g[v].append(u)
g[u].append(v)
dfs(first, g, visited)
return visited[second] == 1
May 27, 2016