-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnionFindTest.java
More file actions
46 lines (42 loc) · 1.1 KB
/
Copy pathUnionFindTest.java
File metadata and controls
46 lines (42 loc) · 1.1 KB
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
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class UnionFindTest
{
// Test client for Union find
private WeightedQuickUnionUF uf;
private Scanner sc;
private int T; // number of test cases
public UnionFindTest()
{
try
{
this.sc = new Scanner(new File("uf_data.txt"));
}
catch (FileNotFoundException e)
{
this.sc = new Scanner(System.in);
}
this.T = sc.nextInt();
this.uf = new WeightedQuickUnionUF(this.T);
}
public void test()
{
for (int i = 0; i < T; i++)
{
int p = sc.nextInt(), q = sc.nextInt();
if (!uf.connected(p, q))
{
System.out.println("Connecting " + p + " and " + q);
uf.union(p, q);
}
else
System.out.println(p + " and " + q + " already connected");
}
}
public static void main(String[] args)
{
UnionFindTest ufTest = new UnionFindTest();
ufTest.test();
}
}