-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
52 lines (51 loc) · 1.32 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
class DSU{
ArrayList<Integer> parent=new ArrayList<>();
ArrayList<Integer> rank=new ArrayList<>();
DSU(int n){
for(int i=0;i<n;i++){
parent.add(i);
rank.add(0);
}
}
int findParent(int node){
if(node==parent.get(node)){
return node;
}
int parentNode=findParent(parent.get(node));
parent.set(node,parentNode);
return parent.get(node);
}
void unionByRank(int u,int v){
int parentU=findParent(u);
int parentV=findParent(v);
if(parentU==parentV){
return;
}else{
if(rank.get(parentU)<rank.get(parentV)){
parent.set(parentU,parentV);
}else if(rank.get(parentU)>rank.get(parentV)){
parent.set(parentV,parentU);
}else{
parent.set(parentV,parentU);
rank.set(parentU,rank.get(parentU)+1);
}
}
}
}
public class Solution {
public void printNum(int i,int n){
if(i>n){
return;
}
System.out.print(i+" ");
printNum(i+1,n);
}
public static void main(String[] args) {
int i=1;
int n=10;
Solution s=new Solution();
s.printNum(i,n);
System.out.println((char)59);
}
}