leetcode 890. 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:

Input: N = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4], 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
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 which dislikes[i] == dislikes[j].

思路:二分图染色

class Solution {
public:
    vector<int>G[2100];
    int color[2100];
    //memset(color, -1, sizeof (color));
    int bfs() {
        queue<int>q;
        q.push(1);
        color[1] = 1;
        while(!q.empty()) {
            int v1 = q.front();
            q.pop();
            for(int i = 0; i < G[v1].size(); i++) {
                int v2 = G[v1][i];
                if(color[v2] == -1) {
                    color[v2] = -color[v1];
                    q.push(v2);
                }
                else if(color[v1] == color[v2])
                    return 0;
            }
        }
        return 1;
    }
    bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
        for (int i = 0; i < dislikes.size(); ++i) {
            vector<int> x = dislikes[i];
            //cout << x[0]  << " " <<x[1] << endl;
            G[x[0]].push_back(x[1]);
            G[x[1]].push_back(x[0]);
        }
        for (int i = 0; i <= 2000; ++i)color[i] = -1;
        return bfs();
    }
};

原文地址:https://www.cnblogs.com/pk28/p/9462393.html

时间: 2024-10-31 05:16:04

leetcode 890. Possible Bipartition的相关文章

[LeetCode] 890. Find and Replace Pattern 查找和替换模式

You have a list of?words?and a?pattern, and you want to know which words in?words?matches the pattern. A word matches the pattern if there exists a permutation of letters?p?so that after replacing every letter?x?in the pattern with?p(x), we get the d

arts-week6

Algorithm 830. Positions of Large Groups - LeetCode 75. Sort Colors - LeetCode 859. Buddy Strings - LeetCode 791. Custom Sort String - LeetCode 804. Unique Morse Code Words - LeetCode 877. Stone Game - LeetCode 890. Find and Replace Pattern - LeetCod

890. Find and Replace Pattern - LeetCode

Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" abc -> 011 abc -> 012 deq -> 0

[LeetCode] 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

LeetCode刷题总结-字符串篇

本文梳理对LeetCode上有关字符串习题的知识点,并给出对应的刷题建议.本文建议刷题的总数为32题.具体知识点如下图: 1.回文问题 题号:5. 最长回文子串,难度中等 题号:214. 最短回文串,难度困难 题号:564. 寻找最近的回文数,难度困难 2.子串问题(类似子集) 题号:76. 最小覆盖子串,难度困难 题号:115. 不同的子序列,难度困难 题号:522. 最长特殊序列 II,难度中等 题号:1163. 按字典序排在最后的子串,难度困难 3.表达式求值问题 题号:12. 整数转罗马

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个