-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnionByRank.java
173 lines (135 loc) · 3.55 KB
/
UnionByRank.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
*
*/
package com.test.graphs;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* @author Anurag
*
*/
public class UnionByRank {
private Map<Long, Node> map = new HashMap<>();
class Node {
long data;
Node parent;
int rank;
}
/**
* Create a set with only one element.
*/
public void makeSet(long data) {
Node node = new Node();
node.data = data;
node.parent = node;
node.rank = 0;
map.put(data, node);
}
/**
* Combines two sets together to one. Does union by rank
*
* @return true if data1 and data2 are in different set before union else
* false.
*/
public boolean union(long data1, long data2) {
// both will have parent for sure---
Node node1 = map.get(data1);
Node node2 = map.get(data2);
// both parents will be returned---
Node parent1 = findSet(node1);
Node parent2 = findSet(node2);
// if they are part of same set do nothing
if (parent1.data == parent2.data) {
return false;
}
// else whoever's rank is higher becomes parent of other
if (parent1.rank >= parent2.rank) {
// increment rank only if both sets have same rank
parent1.rank = (parent1.rank == parent2.rank) ? parent1.rank + 1 : parent1.rank;
parent2.parent = parent1;
} else {
parent1.parent = parent2;
}
return true;
}
/**
* Finds the representative of this set
*/
public long findSet(long data) {
return findSet(map.get(data)).data;
}
/**
* Find the representative recursively and does path compression as well.
*/
private Node findSet(Node node) {
Node parent = node.parent;
if (parent == node) {
return parent;
}
node.parent = findSet(node.parent);
return node.parent;
}
/**
* @param args
*/
public static void main(String[] args) {
UnionByRank solution = new UnionByRank();
File file = new File("D://input.txt");
Scanner in = null;
try {
in = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String query = in.nextLine();
String[] subQuery = query.split(" ");
int N = Integer.parseInt(subQuery[0]);
int P = Integer.parseInt(subQuery[1]);
if (N >= 1 && N <= Math.pow(10, 5)) {
for (int count = 0; count < N; count++) {
solution.makeSet(count);
}
if (P >= 1 && P <= Math.pow(10, 4)) {
for (int count = 0; count < P; count++) {
String set = in.nextLine();
String[] subSet = set.split(" ");
int A = Integer.parseInt(subSet[0]);
int B = Integer.parseInt(subSet[1]);
if (A >= 0 && A <= N - 1 && B >= 0 && B <= N - 1) {
solution.union(A, B);
}
}
}
// output the sum of different pairs possible
solution.outputSet(N);
}
}
private void outputSet(int n) {
Map<Long, Integer> sum = new HashMap<Long, Integer>();
for (int count = 0; count < n; count++) {
Node node = map.get(Long.valueOf(count));
Node parent = findSet(node);
if (sum.get(parent.data) != null) {
int number = sum.get(parent.data);
sum.put(parent.data, ++number);
} else {
sum.put(parent.data, 1);
}
}
List<Integer> list = new ArrayList<Integer>(sum.values());
long output = 0;
long result = 0;
for(int size : list)
{
result += output*size;
output += size;
}
System.out.println(result);
}
}