← 返回 google 的题目列表Earliest Time When Everyone Becomes Friends (Union-Find / Graph) + Support Unfriend
类型:online_judge
Problem
You have n people labeled 0..n-1 and a stream of time-stamped social events.
Part 1 (Basic)
Each event is (t, a, b) meaning that at time t, a and b become friends.
Return the earliest time t such that from that moment on, everyone is in a single connected friend group (i.e., the graph is connected). If this never happens, return -1.
Part 2 (Follow-up: support unfriend)
The stream may also include an unfriending event: (t, a, b, "unfriend").
Design an approach to return the earliest time when the graph becomes fully connected (if ever), while processing events in time order. You may choose an offline solution if needed.
Constraints (typical interview assumptions)
1 <= n <= 1e5
number of events m up to 1e5
timestamps are sortable
Example
Input:
n = 4
events = [(1,0,1), (2,1,2), (3,2,3)]
Output: 3