-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
38 lines (36 loc) · 947 Bytes
/
Main.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
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
int[] parent;
int[] rank;
public int countComponents(int n, int[][] edges) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = i;
rank = new int[n];
for (int[] edge : edges){
if(union(edge[0], edge[1]))
n--;
}
return n;
}
public int find(int x){
if(parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
public boolean union(int x, int y){
int rootX = find(x), rootY = find(y);
if(rootX == rootY) return false;
if(rank[rootX] >= rank[rootY]){
parent[rootY] = rootX;
if(rank[rootX] == rank[rootY])
rank[rootX]++;
}
else
parent[rootX] = rootY;
return true;
}
}