← 返回 google 的题目列表Earliest Time When All People Become Connected
类型:online_judge
Given n people labeled from 0 to n - 1 and a list of friendship logs, where logs[i] = [timestamp, x, y] means that people x and y became friends at timestamp.
Friendship is transitive: if a is connected to b and b is connected to c, then all three people belong to the same friend group.
Return the earliest timestamp at which all n people are connected in one group. Return -1 if this never happens.
Input Format
n m
timestamp_1 x_1 y_1
timestamp_2 x_2 y_2
...
timestamp_m x_m y_m
The logs are not necessarily sorted by timestamp.
Output Format
Print the earliest timestamp when everyone becomes connected, or -1 if they never do.
Example 1
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
Constraints
2 <= n <= 100
1 <= m <= 10^4
0 <= x, y < n, x != y
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