> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Possible Bipartition Python Solution

> Tested Python solution for LeetCode 886 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 886, [Medium](/catalog/medium). Topics: [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Union-Find](/catalog/topics/union-find), [Graph Theory](/catalog/topics/graph-theory), Graph Coloring, Bipartite Graph. [View on LeetCode](https://leetcode.com/problems/possible-bipartition/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 886   # by problem number
lcpy gen -s possible_bipartition   # by problem name
```

## Problem

We want to split a group of `n` people (labeled from `1` to `n`) into two groups of **any size**. Each person may dislike some other people, and they should not go into the same group.

Given the integer `n` and the array `dislikes` where `dislikes[i] = [ai, bi]` indicates that the person labeled `ai` does not like the person labeled `bi`, return `true` if it is possible to split everyone into two groups in this way.

### Examples

```
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
```

**Explanation:** The first group has \[1,4], and the second group has \[2,3].

```
Input: n = 3, dislikes = [[1,2],[1,3],[2,3]]
Output: false
```

**Explanation:** We need at least 3 groups to divide them. We cannot put them in two groups.

### Constraints

* `1 <= n <= 2000`
* `0 <= dislikes.length <= 10^4`
* `dislikes[i].length == 2`
* `1 <= ai < bi <= n`
* All the pairs of dislikes are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/possible_bipartition/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/possible_bipartition/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(n + E)
    # Space: O(n + E)
    def possible_bipartition(self, n: int, dislikes: list[list[int]]) -> bool:
        adj: list[list[int]] = [[] for _ in range(n + 1)]
        for a, b in dislikes:
            adj[a].append(b)
            adj[b].append(a)

        color = [0] * (n + 1)
        for start in range(1, n + 1):
            if color[start] != 0:
                continue
            color[start] = 1
            queue = deque([start])
            while queue:
                person = queue.popleft()
                for other in adj[person]:
                    if color[other] == 0:
                        color[other] = -color[person]
                        queue.append(other)
                    elif color[other] == color[person]:
                        return False
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n + E) | O(n + E) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.