|
54 | 54 | <li>对于 <code>dislikes[i] == dislikes[j]</code> 不存在 <code>i != j</code></li>
|
55 | 55 | </ul>
|
56 | 56 |
|
57 |
| - |
58 | 57 | ## 解法
|
59 | 58 |
|
60 | 59 | <!-- 这里可写通用的实现逻辑 -->
|
61 | 60 |
|
| 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 | + |
62 | 120 | <!-- tabs:start -->
|
63 | 121 |
|
64 | 122 | ### **Python3**
|
65 | 123 |
|
66 | 124 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
67 | 125 |
|
68 | 126 | ```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 |
70 | 147 | ```
|
71 | 148 |
|
72 | 149 | ### **Java**
|
73 | 150 |
|
74 | 151 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
75 | 152 |
|
76 | 153 | ```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 | +``` |
77 | 220 |
|
| 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 | +} |
78 | 254 | ```
|
79 | 255 |
|
80 | 256 | ### **...**
|
|
0 commit comments