Skip to content

Commit 1e0ace4

Browse files
committed
package possible_bipartition
1 parent 3e7359c commit 1e0ace4

File tree

5 files changed

+104
-25
lines changed

5 files changed

+104
-25
lines changed

possible-bipartition/UnionFind.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package possible_bipartition
2+
3+
type UnionFind struct {
4+
parents, sizes map[int]int
5+
}
6+
7+
func (u UnionFind) Size(x int) int {
8+
9+
v, o := u.sizes[x]
10+
if o {
11+
return v
12+
} else {
13+
return 1
14+
}
15+
}
16+
func NewUnionFind() UnionFind {
17+
parent := map[int]int{}
18+
19+
return UnionFind{parents: parent, sizes: map[int]int{}}
20+
}
21+
22+
func (u UnionFind) Find(x int) int {
23+
_, o := u.parents[x]
24+
if !o {
25+
u.parents[x] = x
26+
}
27+
if u.parents[x] != x {
28+
u.parents[x] = u.Find(u.parents[x])
29+
}
30+
return u.parents[x]
31+
}
32+
33+
func (u UnionFind) Union(x, y int) {
34+
x, y = u.Find(x), u.Find(y)
35+
if x == y {
36+
return
37+
}
38+
39+
u.sizes[x] = u.Size(x)
40+
u.sizes[y] = u.Size(y)
41+
42+
if u.sizes[x] > u.sizes[y] {
43+
u.parents[y] = x
44+
u.sizes[x] = u.sizes[x] + u.sizes[y]
45+
} else {
46+
u.parents[x] = y
47+
u.sizes[y] = u.sizes[x] + u.sizes[y]
48+
}
49+
}
50+
51+
func (u UnionFind) Connected(x, y int) bool {
52+
return u.Find(x) == u.Find(y)
53+
}

possible-bipartition/export.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package possible_bipartition
2+
3+
func PossibleBipartition(n int, dislikes [][]int) bool {
4+
return possibleBipartition(n, dislikes)
5+
}

possible-bipartition/index.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package possible_bipartition
2+
3+
func possibleBipartition(n int, dislikes [][]int) bool {
4+
g := make([][]int, n)
5+
for _, d := range dislikes {
6+
x, y := d[0]-1, d[1]-1
7+
g[x] = append(g[x], y)
8+
g[y] = append(g[y], x)
9+
}
10+
uf := NewUnionFind()
11+
for x, nodes := range g {
12+
for _, y := range nodes {
13+
uf.Union(nodes[0], y)
14+
if uf.Connected(x, y) {
15+
return false
16+
}
17+
}
18+
}
19+
return true
20+
}

possible-bipartition/index.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package masx200.leetcode_test.possible_bipartition

possible-bipartition/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { UnionFind } from "../largest-component-size-by-common-factor/UnionFind.ts";
2-
export default function possibleBipartition(
3-
n: number,
4-
dislikes: number[][]
5-
): boolean {
6-
const uf = new UnionFind();
7-
8-
const e = Array<Array<number>>(n + 1);
9-
10-
for (const [a, b] of dislikes) {
11-
e[a] ??= [];
12-
e[b] ??= [];
13-
e[a].push(b);
14-
e[b].push(a);
15-
}
16-
17-
for (const i of Object.keys(e)) {
18-
for (const d of e[Number(i)]) {
19-
uf.union(d, e[Number(i)][0]);
20-
if (uf.connected(d, Number(i))) return false;
21-
}
22-
}
23-
24-
return true;
25-
}
1+
import { UnionFind } from "../largest-component-size-by-common-factor/UnionFind.ts";
2+
export default function possibleBipartition(
3+
n: number,
4+
dislikes: number[][],
5+
): boolean {
6+
const uf = new UnionFind();
7+
8+
const e = Array<Array<number>>(n + 1);
9+
10+
for (const [a, b] of dislikes) {
11+
e[a] ??= [];
12+
e[b] ??= [];
13+
e[a].push(b);
14+
e[b].push(a);
15+
}
16+
17+
for (const i of Object.keys(e)) {
18+
for (const d of e[Number(i)]) {
19+
uf.union(d, e[Number(i)][0]);
20+
if (uf.connected(d, Number(i))) return false;
21+
}
22+
}
23+
24+
return true;
25+
}

0 commit comments

Comments
 (0)