File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .company ;
2
+
3
+ public class quickfind {
4
+ private int [] id ;
5
+ public quickfind (int n ) {
6
+ id =new int [n ];
7
+ for (int i =0 ;i <n ;i ++){
8
+ id [i ]=i ;
9
+ }
10
+ }
11
+ public boolean connected (int p ,int q ){
12
+ return id [p ]==id [q ];
13
+ }
14
+ public void union (int p ,int q ){
15
+ int pid =id [p ];
16
+ int qid =id [q ];
17
+ for (int i =0 ;i <id .length ;i ++){
18
+ if (id [i ]==pid ){
19
+ id [i ]=qid ;
20
+ }
21
+ }
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .company ;
2
+
3
+ public class quickunion {
4
+ private int [] id ;
5
+ public void QuickUnion (int n ){
6
+ id =new int [n ];
7
+ for (int i =0 ;i <n ;i ++){
8
+ id [i ]=i ;
9
+ }
10
+ }
11
+ public int root (int n ){
12
+ while (n !=id [n ]){
13
+ n =id [n ];
14
+ }
15
+ return n ;
16
+ }
17
+ public boolean connected (int p ,int q ){
18
+ return root (p )==root (q );
19
+ }
20
+ public void union (int p ,int q ){
21
+ int proot =root (p );
22
+ int qroot =root (q );
23
+ id [proot ]=q ;
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments