-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10608.java
79 lines (58 loc) · 1.4 KB
/
10608.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
/*
* UVa 10608: Friends
*
* Problem Statement: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1549
*/
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int[] people;
public static int[] rank;
public static int max;
public static void init(int n) {
people = new int[n + 1];
rank = new int[n + 1];
for (int i = 0; i < people.length; i++) {
people[i] = i;
rank[i] = 1;
}
max = 1;
}
public static int find(int x) {
while (people[x] != x) {
x = people[x];
}
return x;
}
public static void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA != rootB) {
if (rank[rootA] < rank[rootB]) {
people[rootA] = rootB;
rank[rootB] += rank[rootA];
max = (rank[rootB] > max) ? rank[rootB] : max;
} else {
people[rootB] = rootA;
rank[rootA] += rank[rootB];
max = (rank[rootA] > max) ? rank[rootA] : max;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int n, m;
while (t-- > 0) {
n = scan.nextInt();
m = scan.nextInt();
init(n);
while (m-- > 0) {
union(scan.nextInt(), scan.nextInt());
System.out.println(Arrays.toString(people));
System.out.println(Arrays.toString(rank));
}
System.out.println(max);
}
}
}