Network connection timestamp

Write codeMedium25 minMeta style

Your task

A social network has n people labeled 0..n-1, all initially strangers. You get logs where each log is [timestamp, a, b] meaning a and b became acquainted at that timestamp; acquaintance is mutual and transitive (friends of friends are connected). Return the earliest timestamp at which every person is connected to every other person (directly or transitively). If they never all become connected, return -1. With only one person (n = 1) there is no one to connect to, so return -1. Timestamps are non-negative integers and may be unsorted.

Examples

earliest_all_connected(n: int, logs: list[list[int]]) -> int
Example 1
Inputearliest_all_connected(4, [[20, 0, 1], [5, 2, 3], [10, 1, 2], [30, 0, 3]])
Output20