Skip to content

Latest commit

 

History

History
190 lines (156 loc) · 4.45 KB

File metadata and controls

190 lines (156 loc) · 4.45 KB

中文文档

Description

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.

Solutions

Union find.

Python3

class Solution:
    def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool:
        p = list(range(n))

        def find(x):
            if p[x] != x:
                p[x] = find(p[x])
            return p[x]
        
        dis = defaultdict(list)
        for a, b in dislikes:
            a, b = a - 1, b - 1
            dis[a].append(b)
            dis[b].append(a)
        
        for i in range(n):
            for j in dis[i]:
                if find(i) == find(j):
                    return False
                p[find(j)] = find(dis[i][0])
        return True

Java

class Solution {
    private int[] p;

    public boolean possibleBipartition(int n, int[][] dislikes) {
        p = new int[n];
        List<Integer>[] dis = new List[n];
        for (int i = 0; i < n; ++i) {
            p[i] = i;
            dis[i] = new ArrayList<>();
        }
        for (int[] d : dislikes) {
            int a = d[0] - 1, b = d[1] - 1;
            dis[a].add(b);
            dis[b].add(a);
        }
        for (int i = 0; i < n; ++i) {
            for (int j : dis[i]) {
                if (find(i) == find(j)) {
                    return false;
                }
                p[find(j)] = find(dis[i].get(0));
            }
        }
        return true;
    }

    private int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

C++

class Solution {
public:
    vector<int> p;

    bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
        p.resize(n);
        for (int i = 0; i < n; ++i) p[i] = i;
        unordered_map<int, vector<int>> dis;
        for (auto& d : dislikes)
        {
            int a = d[0] - 1, b = d[1] - 1;
            dis[a].push_back(b);
            dis[b].push_back(a);
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j : dis[i])
            {
                if (find(i) == find(j)) return false;
                p[find(j)] = find(dis[i][0]);
            }
        }
        return true;
    }

    int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
};

Go

func possibleBipartition(n int, dislikes [][]int) bool {
	p := make([]int, n)
	dis := make([][]int, n)
	for i := range p {
		p[i] = i
	}
	var find func(x int) int
	find = func(x int) int {
		if p[x] != x {
			p[x] = find(p[x])
		}
		return p[x]
	}
	for _, d := range dislikes {
		a, b := d[0]-1, d[1]-1
		dis[a] = append(dis[a], b)
		dis[b] = append(dis[b], a)
	}
	for i := 0; i < n; i++ {
		for _, j := range dis[i] {
			if find(i) == find(j) {
				return false
			}
			p[find(j)] = find(dis[i][0])
		}
	}
	return true
}

...