Find the minimum time needed until a graph has at least connected components, given that edges can be activated over time.
You are given an undirected graph with vertices and a set of edges. Each edge becomes available at a certain time. Once the time reaches that value, the edge can be used to connect its endpoints.
Your task is to determine the minimum time such that, using only the edges with availability time , the graph has at least connected components.
If the graph already has at least connected components before any edge is available, the answer is the smallest possible time. If it is impossible to reach at least connected components under the given rules, return the closest valid sentinel value for the platform context if one is specified; otherwise, reason about the feasibility from the available edges.
This is a connectivity-feasibility problem where the key challenge is to efficiently test how many connected components remain after considering all edges up to a given time.
n and k.k.Example 1
Input
n = 4, k = 2 edges = [[1,2,1],[2,3,3],[3,4,5]]
Output
3
Explanation
At time 1, there are 3 components: {1,2}, {3}, {4}. At time 3, edges (1,2) and (2,3) are available, so the graph has 2 components: {1,2,3} and {4}. This is the earliest time when the number of connected components is at least 2.
Example 2
Input
n = 3, k = 3 edges = [[1,2,4],[2,3,7]]
Output
0
Explanation
Before any edge is available, the graph has 3 connected components, which already satisfies k = 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.