-
Notifications
You must be signed in to change notification settings - Fork 20
/
city-and-flood.java
48 lines (44 loc) · 1.03 KB
/
city-and-flood.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
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
UF uf = new UF(n);
for (int i = 0; i < k; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
uf.union(a, b);
}
HashSet<Integer> hs = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
hs.add(uf.find(i));
}
System.out.println(hs.size());
}
}
class UF{
int[] parent;
public UF(int n) {
this.parent = new int[n+1];
for (int i = 1; i <= n; i++) parent[i] = i;
}
public void union(int i, int j) {
// System.out.println("i= " + i + ", j= " + j);
int u = find(i);
int v = find(j);
// System.out.println("u= " + u + ", v= " + v);
if (u == v) return;
parent[u] = v;
}
public int find(int i) {
while (parent[i] != i) i = parent[i];
return i;
}
}