886. Possible Bipartition (M)

https://leetcode.com/problems/possible-bipartition/

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.

Example 1:

Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4] and group2 [2,3].

Example 2:

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

Example 3:

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

Constraints:

  • 1 <= n <= 2000

  • 0 <= dislikes.length <= 104

  • dislikes[i].length == 2

  • 1 <= dislikes[i][j] <= n

  • ai < bi

  • All the pairs of dislikes are unique.

Solution:

其实这题考察的就是二分图的判定

如果你把每个人看做图中的节点,相互讨厌的关系看做图中的边,那么 dislikes 数组就可以构成一幅图;

又因为题目说互相讨厌的人不能放在同一组里,相当于图中的所有相邻节点都要放进两个不同的组;

那就回到了「双色问题」,如果能够用两种颜色着色所有节点,且相邻节点颜色都不同,那么你按照颜色把这些节点分成两组不就行了嘛。

所以解法就出来了,我们把 dislikes 构造成一幅图,然后执行二分图的判定算法即可:

这个题目要注意的是 buildGraph要一个双向图,即是undirected

至此,这道题也使用 DFS 算法解决了,如果你想用 BFS 算法,和之前写的解法是完全一样的,可以自己尝试实现。

Last updated

Was this helpful?