-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualFriends.java
79 lines (72 loc) · 2.4 KB
/
VirtualFriends.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
/**
* https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2498
* #dsu
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
class VirtualFriends {
public static int getIndex(
HashMap<String, Integer> hashFriendIndex,
ArrayList<String> peopleArr,
String person,
ArrayList<Integer> parentArr,
ArrayList<Integer> sizeArr) {
if (!hashFriendIndex.containsKey(person)) {
peopleArr.add(person);
int idx = peopleArr.size() - 1;
hashFriendIndex.put(person, idx);
parentArr.add(idx);
sizeArr.add(1);
}
return hashFriendIndex.get(person);
}
public static int findSet(int u, ArrayList<Integer> parentArr) {
if (parentArr.get(u) != u) {
int p = findSet(parentArr.get(u), parentArr);
parentArr.set(u, p);
}
return parentArr.get(u);
}
public static int unionSet(
ArrayList<Integer> parentArr, ArrayList<Integer> sizeArr, int u, int v) {
int up = findSet(u, parentArr);
int vp = findSet(v, parentArr);
if (up == vp) {
return sizeArr.get(up);
}
if (sizeArr.get(up) > sizeArr.get(vp)) {
parentArr.set(vp, up);
sizeArr.set(up, sizeArr.get(up) + sizeArr.get(vp));
return sizeArr.get(up);
} else if (sizeArr.get(up) < sizeArr.get(vp)) {
parentArr.set(up, vp);
sizeArr.set(vp, sizeArr.get(vp) + sizeArr.get(up));
return sizeArr.get(vp);
} else {
parentArr.set(up, vp);
sizeArr.set(vp, sizeArr.get(vp) + sizeArr.get(up));
return sizeArr.get(vp);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for (int t = 0; t < testCases; t++) {
int friendships = sc.nextInt();
String person1, person2;
HashMap<String, Integer> hashFriendIndex = new HashMap<>();
ArrayList<String> peopleArr = new ArrayList<>();
ArrayList<Integer> parentArr = new ArrayList<>();
ArrayList<Integer> sizeArr = new ArrayList<>();
int idx1, idx2, ans;
for (int f = 0; f < friendships; f++) {
person1 = sc.next();
person2 = sc.next();
idx1 = getIndex(hashFriendIndex, peopleArr, person1, parentArr, sizeArr);
idx2 = getIndex(hashFriendIndex, peopleArr, person2, parentArr, sizeArr);
System.out.println(unionSet(parentArr, sizeArr, idx1, idx2));
}
}
}
}