Lv.2bfsbipartitegraph
Split Rivals into Two Teams
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Split n players into two teams so that no rival pair rivals[i] = [a, b] shares a team. Return whether that is possible
(a team may be empty).
Examples
| n | rivals | result |
|---|---|---|
4 | [[0, 1], [1, 2], [2, 3]] | true |
3 | [[0, 1], [1, 2], [2, 0]] | false |
5 | [] | true |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int, rivals: int[][]) → bool
Constraints
-
n,rivals.length - ; pairs may repeat and
a == bmakes it impossible.
Expected complexity
- Time
- O(N + M)
- Space
- O(N + M)
Examples
| n | rivals | Expected |
|---|---|---|
4 | [[0,1],[1,2],[2,3]] | true |
3 | [[0,1],[1,2],[2,0]] | false |
5 | [] | true |
Hint
Hint
Bipartite check: BFS-colour every uncoloured player, giving rivals the opposite colour; a rival with the same colour means
false. Trying all splits exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1488 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 9 | verdict only |
| performance | 60 | 4 | verdict only |