第22天
今天的题目是 Possible Bipartition :
Given a set of N
people (numbered 1, 2, ..., N
), we would like to split everyone into two groups of any size.
Each person may dislike some other people, and they should not go into the same group.
Formally, if dislikes[i] = [a, b]
, it means it is not allowed to put the people numbered a
and b
into the same group.
Return true
if and only if it is possible to split everyone into two groups in this way.
Example 1:
Example 2:
Example 3:
Note:
1 <= N <= 2000
0 <= dislikes.length <= 10000
1 <= dislikes[i][j] <= N
dislikes[i][0] < dislikes[i][1]
- There does not exist
i != j
for whichdislikes[i] == dislikes[j]
.
又是一道图的题目,而且和昨天的题目思路是一样的,先遍历染色,然后再判断是否满足即可。
这里有些不同的是,这道题给出的输入是边的列表,然后我们需要手动建个图。同时,这道题还可以用在遍历时判断是否已经不符合了,进而可以提前退出。代码如下: