forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Friends.java
166 lines (112 loc) · 4.46 KB
/
Friends.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
// There is a town with N citizens. It is known that some pairs of people are friends. According to the
// famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends
// and B and C are friends then A and C are friends, too.
// Your task is to count how many people there are in the largest group of friends.
// Input
// Input consists of several datasets. The first line of the input consists of a line with the number of test
// cases to follow.
// The first line of each dataset contains tho numbers N and M, where N is the number of town’s
// citizens (1 ≤ N ≤ 30000) and M is the number of pairs of people (0 ≤ M ≤ 500000), which are known
// to be friends. Each of the following M lines consists of two integers A and B (1 ≤ A ≤ N, 1 ≤ B ≤ N,
// A ̸= B) which describe that A and B are friends. There could be repetitions among the given pairs
// Output
// The output for each test case should contain (on a line by itself) one number denoting how many people
// there are in the largest group of friends on a line by itself.
// Sample Input
// 2
// 3 2
// 1 2
// 2 3
// 10 12
// 1 2
// 3 1
// 3 4
// 5 4
// 3 5
// 4 6
// 5 2
// 2 1
// 7 1
// 1 2
// 9 10
// 8 9
// Sample Output
// 3
// 7
import java.io.*;
import java.util.*;
/**
* Created by kdn251 on 2/15/17.
*/
public class Friends {
//initialize globals to track each individual person and their relationships
public static int[] people = new int[30001];
public static int[] relationships = new int[50001];
public static void main(String args[]) throws Exception {
//initialize buffered reader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
//store number of test cases
int testCases = Integer.parseInt(line);
for(int i = 0; i < testCases; i++) {
//determine number of people and pairs of people (N and M)
String[] info = br.readLine().split(" ");
int numberOfPeople = Integer.parseInt(info[0]);
int numberOfRelationship = Integer.parseInt(info[1]);
startUnion(numberOfPeople, people, relationships);
//iterate through all relationships
for(int j = 0; j < numberOfRelationship ; j++) {
//split current line to determine person and friend
String[] currentLine = br.readLine().split(" ");
int person = Integer.parseInt(currentLine[0]);
int friend = Integer.parseInt(currentLine[1]);
union(person, friend);
}
//initialize maxGroup to one because each group has one person initially
int maxGroup = 1;
//iterate through relationships to find the largest group
for(int j = 0; j <= numberOfPeople; j++) {
//update max as needed
maxGroup = relationships[j] > maxGroup ? relationships[j] : maxGroup;
}
//print result
System.out.println(maxGroup);
}
}
public static void startUnion(int numberOfPeople, int[] people, int[] relationships) {
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 void union(int person, int friend) {
//find parents in tree
person = find(person);
friend = find(friend);
if(person != friend) {
//add connection between person and friend
join(person, friend);
}
}
public static int find(int person) {
//traverse parents of tree if possible
if(people[person] != person) {
people[person] = find(people[person]);
}
return people[person];
}
public static void join(int person, int 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;
}
else {
relationships[friend] += relationships[person];
people[person] = friend;
}
}
}
//source: https://github.com/morris821028/UVa/blob/master/volume106/10608%20-%20Friends.cpp#L27