Skip to content

Commit f108f8b

Browse files
committed
feat: add solutions to lc problem: No.0886.Possible Bipartition
1 parent c0f6a38 commit f108f8b

File tree

23 files changed

+1304
-1660
lines changed

23 files changed

+1304
-1660
lines changed

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<script>
2727
window.$docsify = {
2828
name: 'leetcode',
29+
repo: 'doocs/leetcode',
2930
logo: '/images/doocs-leetcode.png',
3031
search: [
3132
'/', '/solution/', '/lcof/', '/lcof2/', '/lcci/', '/lcs/', '/lcp/', '/basic/'

solution/0800-0899/0886.Possible Bipartition/README.md

+178-2
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,203 @@
5454
<li>对于 <code>dislikes[i] == dislikes[j]</code> 不存在 <code>i != j</code></li>
5555
</ul>
5656

57-
5857
## 解法
5958

6059
<!-- 这里可写通用的实现逻辑 -->
6160

61+
并查集。
62+
63+
模板 1——朴素并查集:
64+
65+
```python
66+
# 初始化,p存储每个点的父节点
67+
p = list(range(n))
68+
69+
# 返回x的祖宗节点
70+
def find(x):
71+
if p[x] != x:
72+
# 路径压缩
73+
p[x] = find(p[x])
74+
return p[x]
75+
76+
# 合并a和b所在的两个集合
77+
p[find(a)] = find(b)
78+
```
79+
80+
模板 2——维护 size 的并查集:
81+
82+
```python
83+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
84+
p = list(range(n))
85+
size = [1] * n
86+
87+
# 返回x的祖宗节点
88+
def find(x):
89+
if p[x] != x:
90+
# 路径压缩
91+
p[x] = find(p[x])
92+
return p[x]
93+
94+
# 合并a和b所在的两个集合
95+
if find(a) != find(b):
96+
size[find(b)] += size[find(a)]
97+
p[find(a)] = find(b)
98+
```
99+
100+
模板 3——维护到祖宗节点距离的并查集:
101+
x
102+
```python
103+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
104+
p = list(range(n))
105+
d = [0] * n
106+
107+
# 返回x的祖宗节点
108+
def find(x):
109+
if p[x] != x:
110+
t = find(p[x])
111+
d[x] += d[p[x]]
112+
p[x] = t
113+
return p[x]
114+
115+
# 合并a和b所在的两个集合
116+
p[find(a)] = find(b)
117+
d[find(a)] = distance
118+
```
119+
62120
<!-- tabs:start -->
63121

64122
### **Python3**
65123

66124
<!-- 这里可写当前语言的特殊实现逻辑 -->
67125

68126
```python
69-
127+
class Solution:
128+
def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool:
129+
p = list(range(n))
130+
131+
def find(x):
132+
if p[x] != x:
133+
p[x] = find(p[x])
134+
return p[x]
135+
136+
mp = collections.defaultdict(list)
137+
for i, j in dislikes:
138+
mp[i - 1].append(j - 1)
139+
mp[j - 1].append(i - 1)
140+
for i in range(n):
141+
dis = mp[i]
142+
for j in dis:
143+
if find(i) == find(j):
144+
return False
145+
p[find(j)] = find(dis[0])
146+
return True
70147
```
71148

72149
### **Java**
73150

74151
<!-- 这里可写当前语言的特殊实现逻辑 -->
75152

76153
```java
154+
class Solution {
155+
public:
156+
vector<int> p;
157+
158+
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
159+
p.resize(n);
160+
for (int i = 0; i < n; ++i) p[i] = i;
161+
unordered_map<int, vector<int>> mp;
162+
for (auto e : dislikes)
163+
{
164+
mp[e[0] - 1].push_back(e[1] - 1);
165+
mp[e[1] - 1].push_back(e[0] - 1);
166+
}
167+
for (int i = 0; i < n; ++i)
168+
{
169+
auto dis = mp[i];
170+
for (int j : dis)
171+
{
172+
if (find(i) == find(j)) return false;
173+
p[find(j)] = find(dis[0]);
174+
}
175+
}
176+
return true;
177+
}
178+
179+
int find(int x) {
180+
if (p[x] != x) p[x] = find(p[x]);
181+
return p[x];
182+
}
183+
};
184+
```
185+
186+
### **C++**
187+
188+
```cpp
189+
class Solution {
190+
public:
191+
vector<int> p;
192+
193+
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
194+
p.resize(n);
195+
for (int i = 0; i < n; ++i) p[i] = i;
196+
unordered_map<int, vector<int>> mp;
197+
for (auto e : dislikes)
198+
{
199+
mp[e[0] - 1].push_back(e[1] - 1);
200+
mp[e[1] - 1].push_back(e[0] - 1);
201+
}
202+
for (int i = 0; i < n; ++i)
203+
{
204+
auto dis = mp[i];
205+
for (int j : dis)
206+
{
207+
if (find(i) == find(j)) return false;
208+
p[find(j)] = find(dis[0]);
209+
}
210+
}
211+
return true;
212+
}
213+
214+
int find(int x) {
215+
if (p[x] != x) p[x] = find(p[x]);
216+
return p[x];
217+
}
218+
};
219+
```
77220

221+
### **Go**
222+
223+
```go
224+
var p []int
225+
226+
func possibleBipartition(n int, dislikes [][]int) bool {
227+
p = make([]int, n)
228+
for i := 0; i < n; i++ {
229+
p[i] = i
230+
}
231+
mp := make(map[int][]int)
232+
for _, e := range dislikes {
233+
mp[e[0]-1] = append(mp[e[0]-1], e[1]-1)
234+
mp[e[1]-1] = append(mp[e[1]-1], e[0]-1)
235+
}
236+
for i := 0; i < n; i++ {
237+
dis := mp[i]
238+
for _, j := range dis {
239+
if find(i) == find(j) {
240+
return false
241+
}
242+
p[find(j)] = find(dis[0])
243+
}
244+
}
245+
return true
246+
}
247+
248+
func find(x int) int {
249+
if p[x] != x {
250+
p[x] = find(p[x])
251+
}
252+
return p[x]
253+
}
78254
```
79255

80256
### **...**

0 commit comments

Comments
 (0)