First Moment a Cable Network Becomes One Piece
Trees & GraphsAuto-graded · 17 test casesLast updated
Replay unsorted timed link events and find the earliest timestamp at which every node is in one connected group, using union-find over events sorted by time.
Network Fully Linked
A network has n machines numbered 0 through n - 1. At the start, no machine is linked to any other.
You receive a list links. Each entry is [time, a, b] and means that at moment time, a two-way link appeared between machine a and machine b. Links never disappear once created.
Two machines are in the same group if you can travel from one to the other by following links that have already appeared. The network is fully linked at a given moment when all n machines are in a single group.
Return the smallest time value such that, using only the links whose time is less than or equal to that value, the network is fully linked. If that never happens, return -1.
Notes
- The list
linksis not sorted by time. - Several links may share the same time, and the same pair of machines may be linked more than once (at equal or different times). A link between machines already in the same group changes nothing.
- A time of
0is a legitimate answer. - The returned value is always one of the
timevalues from the input, or-1.
Constraints
2 <= n <= 10^51 <= len(links) <= 2 * 10^5- Each entry is
[time, a, b]with0 <= time <= 10^9 0 <= a, b < nanda != b
Examples
Example 1
Input: earliest_full_link_time(4, [[20230101, 0, 1], [20230104, 2, 3], [20230102, 1, 2], [20230103, 0, 2]])
Output: 20230104
Example 2
Input: earliest_full_link_time(4, [[5, 0, 1], [10, 1, 2]])
Output: -1
Hints
Hint 1
Once two machines are connected they stay connected, so think about processing the links from earliest to latest and tracking how many groups remain.
Hint 2
A union-find (disjoint set) structure lets you check whether two machines are already in the same group and merge groups in nearly constant time.
Hint 3
Sort by time first. Each successful merge reduces the group count by one; the answer is the time of the merge that brings the count to 1. If that never happens, return -1.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Links only ever add connectivity, so the set of groups at time t is determined by all links with time at most t, and the number of groups never increases as t grows. The answer is therefore the first time the group count reaches 1.
- Sort the links by time.
- Keep a union-find structure with
nsingleton groups and a countergroups = n. - Walk through the sorted links. For each one, find the roots of both machines.
- If the roots are the same, the link is redundant; skip it.
- Otherwise merge the two groups (attach the smaller under the larger) and decrement
groups. - If
groupsis now 1, return the current link's time.
- If the loop ends without reaching one group, return
-1.
The find function is iterative with path halving, which avoids recursion limits for large n and keeps operations nearly constant time.
Why it is correct
- Links are processed in non-decreasing time order, so when the count first hits 1, every link used so far has time at most the current one, and the links before it (strictly earlier in the order) left at least two groups. Hence no earlier time could have worked.
- Ties are harmless: if connectivity completes partway through links with the same time, the returned value is that shared time regardless of their internal order.
- Redundant or duplicate links are skipped because their endpoints share a root, so the counter only drops on real merges.
- Reaching one group requires exactly
n - 1successful merges. If fewer happen (including when there are fewer thann - 1links, or some machine is never mentioned), the counter stays above 1 and the answer is-1.
This is Kruskal's minimum spanning tree algorithm stopped early: the answer is the largest weight (time) in a minimum spanning tree.
Complexity
With m = len(links):
- Time:
O(m log m)for sorting plusO(m * alpha(n))for union-find, so sorting dominates. - Space:
O(n)for the union-find arrays plusO(m)for the sorted copy.
Reference solution
from typing import List
def earliest_full_link_time(n: int, links: List[List[int]]) -> int:
parent = list(range(n))
size = [1] * n
groups = n
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for time, a, b in sorted(links, key=lambda link: link[0]):
root_a = find(a)
root_b = find(b)
if root_a == root_b:
continue
if size[root_a] < size[root_b]:
root_a, root_b = root_b, root_a
parent[root_b] = root_a
size[root_a] += size[root_b]
groups -= 1
if groups == 1:
return time
return -1