-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendNetwork.java
More file actions
75 lines (62 loc) · 1.69 KB
/
FriendNetwork.java
File metadata and controls
75 lines (62 loc) · 1.69 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
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
package CodeTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
//그래프, 해시
//Union - Find
//백준 4195번
public class FriendNetwork {
static int testcase,F;
static int[] parent, cnt;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
StringTokenizer st;
HashMap<String,Integer> hm;
testcase = Integer.parseInt(br.readLine());
while(testcase-->0){
F = Integer.parseInt(br.readLine());
hm = new HashMap<>();
cnt = new int[F*2+1];
parent = new int[F*2+1];
int idx=0;
while(F-->0){
st = new StringTokenizer(br.readLine());
String[] strs = {st.nextToken(),st.nextToken()};
if(!hm.containsKey(strs[0])) {
hm.put(strs[0], idx);
parent[idx] = idx;
cnt[idx++] = 1;
}
if(!hm.containsKey(strs[1])) {
hm.put(strs[1], idx);
parent[idx] = idx;
cnt[idx++] = 1;
}
int x = hm.get(strs[0]);
int y = hm.get(strs[1]);
union(x,y);
sb.append(cnt[find(x)]).append("\n");
}
}
System.out.println(sb.toString());
}
public static int find(int x) {
if(x == parent[x])
return x;
else
return parent[x] = find(parent[x]);
}
public static void union(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if(x != y){
parent[y] = x;
cnt[x] += cnt[y];
}
}
}