Lv.4dfsgraphscc
Strongly Connected Components
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Points 0..n-1 are joined by one-way paths edges[i] = [a, b]. Two points are in the same group if each can reach the other.
Return [number of groups, size of the largest group].
Examples
| n | edges | result |
|---|---|---|
5 | [[0, 1], [1, 2], [2, 0], [2, 3], [3, 4]] | [3, 3] |
4 | [[0, 1], [1, 0], [2, 3], [3, 2], [1, 2]] | [2, 2] |
3 | [] | [3, 1] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int, edges: int[][]) → int[]
Constraints
-
n,edges.length; repeats and self-loops may appear.
Expected complexity
- Time
- O(N + M)
- Space
- O(N + M)
Examples
| n | edges | Expected |
|---|---|---|
5 | [[0,1],[1,2],[2,0],[2,3],[3,4]] | [3,3] |
4 | [[0,1],[1,0],[2,3],[3,2],[1,2]] | [2,2] |
3 | [] | [3,1] |
Hint
Hint
Strongly connected components: Kosaraju (post-order on the graph, then DFS on the reversed graph in reverse post-order) or Tarjan, iteratively. Peeling one component at a time with forward/backward searches is when components are many.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 222 ms | estimated |
| C++ (GCC, gnu++23) | 222 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1414 ms | derived |
| Kotlin 2.4 (JVM) | 1444 ms | estimated |
| C# 14 (.NET 10) | 1444 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 8 | verdict only |
| performance | 60 | 4 | verdict only |