Earliest Time of Full Connectivity
Implement earliest_full_connection(n: int, events: list[list[int]]) -> int.
There are n entities labeled 0 through n - 1. Each event is [timestamp, a, b] and creates a permanent undirected connection between entities a and b. Events may be given in any order. Return the earliest timestamp at which all entities are connected through direct or indirect connections.
Input Domain
-
1 <= n <= 200,000
.
-
0 <= len(events) <= 300,000
.
-
Timestamps fit in signed 64-bit integers.
-
Every endpoint is a valid entity label; self-connections and repeated connections may occur.
Output Rules
-
All events with the same timestamp take effect as one time group.
-
Return the timestamp of the first group after which there is exactly one connected component.
-
Return
0
when
n = 1
, even if there are no events.
-
Return
-1
if full connectivity never occurs.
Constraints
-
Ordering among events with equal timestamps must not change the answer.
-
Target time is
O(m log m + m alpha(n))
for
m
events.
Examples
Example 1
Input: n = 4, events = [[5,0,1],[2,2,3],[7,1,2]]
Output: 7
Example 2
Input: n = 3, events = [[4,0,1],[4,1,0]]
Output: -1