forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualFriends.java
112 lines (97 loc) · 3.93 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
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
// These days, you can do all sorts of things online. For example, you can use various websites to make
// virtual friends. For some people, growing their social network (their friends, their friends’ friends, their
// friends’ friends’ friends, and so on), has become an addictive hobby. Just as some people collect stamps,
// other people collect virtual friends.
// Your task is to observe the interactions on such a website and keep track of the size of each person’s
// network.
// Assume that every friendship is mutual. If Fred is Barney’s friend, then Barney is also Fred’s friend.
// Input
// The first line of input contains one integer specifying the number of test cases to follow. Each test case
// begins with a line containing an integer F, the number of friendships formed, which is no more than
// 100 000. Each of the following F lines contains the names of two people who have just become friends,
// separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
// Output
// Whenever a friendship is formed, print a line containing one integer, the number of people in the social
// network of the two people who have just become friends
// Sample Input
// 1
// 3
// Fred Barney
// Barney Betty
// Betty Wilma
// Sample Output
// 2
// 3
// 4
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
/**
* Created by kdn251 on 3/7/17.
*/
public class VirtualFriends {
public static int[] people = new int[1000001];
public static int[] relationships = new int[1000001];
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
while(cases-- > 0) {
startUnion(people.length);
HashMap<String, Integer> map = new HashMap<String, Integer>();
int friendships = Integer.parseInt(br.readLine());
int numberOfPeople = 0;
for(int i = 0; i < friendships; i++) {
String[] line = br.readLine().split("\\s+");
String x = line[0];
String y = line[1];
if (x.equals(y)) {
System.out.println(1);
continue;
}
if (!map.containsKey(x)) {
map.put(x, ++numberOfPeople);
}
if (!map.containsKey(y)) {
map.put(y, ++numberOfPeople);
}
//print answer for current test case
System.out.println(union(map.get(x), map.get(y)));
}
}
}
public static void startUnion(int numberOfPeople) {
for(int i = 0; i < numberOfPeople; i++) {
//initialize each individual person
people[i] = i;
//each person initially has a group of one (themselves)
relationships[i] = 1;
}
}
public static int union(int person, int friend) {
//find parents in tree
person = find(person);
friend = find(friend);
if(person != friend) {
//add connection between person and friend
//find larger group of the two and make sure both person and friend point to it
if(relationships[person] > relationships[friend]) {
relationships[person] += relationships[friend];
people[friend] = person;
return relationships[person];
}
else {
relationships[friend] += relationships[person];
people[person] = friend;
return relationships[friend];
}
}
return relationships[person];
}
public static int find(int person) {
//traverse parents of tree if possible
if(people[person] != person) {
people[person] = find(people[person]);
}
return people[person];
}
}