You are viewing a single comment's thread. Return to all comments →
my really short and concise solution :
def roadsAndLibraries(n, c_lib, c_road, cities): if c_lib < c_road: return c_lib * n g, seen, ans = ddict(set), set(), 0 for i,j in cities: g[i].add(j) g[j].add(i) def dfs(i): seen.add(i) return sum(dfs(j) for j in g[i] if j not in seen) + 1 return sum((dfs(i) - 1) * c_road + c_lib for i in range(1, n + 1) if i not in seen)
Roads and Libraries
You are viewing a single comment's thread. Return to all comments →
my really short and concise solution :