← 返回 uber 的题目列表The Earliest Moment When Everyone Become Friends
类型:online_judge
Problem: The Earliest Moment When Everyone Become Friends
There are n people labeled from 0 to n - 1. You are given an array logs, where each log is:
[timestamp, a, b]
It means that person a and person b become friends at time timestamp. Friendship is transitive: if a is connected to b, and b is connected to c, then a and c are in the same friend group.
Return the earliest timestamp when all n people become connected. If it never happens, return -1.
Input Format
The first line contains two integers n and m, where n is the number of people and m is the number of logs.
The next m lines each contain three integers:
timestamp a b
Output Format
Print one integer: the earliest timestamp when everyone is connected, or -1 if impossible.
Constraints
2 <= n <= 10^5
0 <= m <= 2 * 10^5
0 <= timestamp <= 10^9
0 <= a, b < n
a != b
Follow-up
If there are blocked nodes or blocked edges that prevent traversal or direct connection, first clarify what “blocked” means, then use BFS/DFS to check reachability after excluding the blocked elements.
Example
Input
6 8
20190101 0 1
20190104 3 4
20190107 2 3
20190211 1 5
20190224 2 4
20190301 0 3
20190312 1 2
20190322 4 5
Output
20190301