← 返回 uber 的题目列表Earliest Timestamp When All Users Become Connected (Ride Sharing Logs)
类型:online_judge
Problem: Earliest Time When All Users Become Connected (Ride-Share Logs)
You are given a list of users and a set of plain-text log entries. Each log entry indicates that at a certain timestamp, user A and user B shared a ride. Once two users share a ride at least once, they are considered connected by an undirected edge. Connectivity is transitive.
Return the earliest timestamp at which all users become connected (i.e., the entire user graph becomes a single connected component) as logs are applied over time.
If they never all become connected, return -1.
Input
Line 1: two integers n m
n: number of users labeled 0..n-1
m: number of log entries
Next m lines: three integers t a b
t: timestamp
a, b: the two users who shared a ride
Output
A single integer: the earliest timestamp when everyone becomes connected; or -1 if impossible.
Constraints / Notes
1 <= n <= 2*10^5
0 <= m <= 2*10^5
0 <= a,b < n, a != b
0 <= t <= 10^9
Logs are not guaranteed to be sorted by timestamp; if multiple logs share the same timestamp, all of them should be applied for that time.
Example
For n=4 and logs (1,0,1), (2,2,3), (3,1,2), everyone becomes connected at t=3, so output 3.
Example
Input
4 3
1 0 1
2 2 3
3 1 2
Output
3