#402 · Trees · Advanced · 145 XP · about 30 min
A company's n offices, numbered 0 to n - 1, are linked by the n - 1 two-way cables in cables, each a pair [a, b], so that there is exactly one route between any two offices: the network is a tree. The main server will go in one office. A message from the server crosses some number of cables to reach each office, and the server's delay is the largest of these numbers.
Write best_roots(n, cables) that returns a sorted list of every office where the server's delay would be as small as possible. In tree words, these are the roots that give the tree its smallest height.
best_roots(4, [[1, 0], [1, 2], [1, 3]]) # returns [1] best_roots(6, [[3, 0], [3, 1], [3, 2], [3, 4], [5, 4]]) # returns [3, 4] best_roots(1, []) # returns [0]
In the second network, a server in office 3 or office 4 reaches every office within 2 cables, while from any other office some office is 3 cables away.
1 <= n <= 20,000, and the cables always form a tree.First solves only, ranked by stars and then time.