File tree Expand file tree Collapse file tree 5 files changed +104
-25
lines changed Expand file tree Collapse file tree 5 files changed +104
-25
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package possible_bipartition
2
+
3
+ func PossibleBipartition (n int , dislikes [][]int ) bool {
4
+ return possibleBipartition (n , dislikes )
5
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package masx200.leetcode_test.possible_bipartition
Original file line number Diff line number Diff line change 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
+ }
You can’t perform that action at this time.
0 commit comments