-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSU.java
62 lines (49 loc) · 1.09 KB
/
DSU.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
53
54
55
56
57
58
59
60
61
62
public class DSU
{
int parent[];
int size[];
int marks[];
int count;
public DSU(int elems) {
parent = new int[elems+1];
size = new int[elems+1];
marks = new int[elems+1];
count = 0;
}
public int findSet(int n) {
if (parent[n] != n) {
parent[n] = findSet(parent[n]);
}
return parent[n];
}
public void setMark(int s, int value) {
marks[ findSet(s) ] = value;
}
public int getMark(int s) {
return marks[ findSet(s) ];
}
public int count() {
return count;
}
public void unionSet(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return;
if (size[a] > size[b]) {
int tmp = a;
a = b;
b = tmp;
}
parent[a] = b;
size[b] += size[a];
count--;
}
public void makeSet(int n) {
if (parent[n] == 0) {
parent[n] = n;
size[n] = 1;
count++;
}
}
}