-
Notifications
You must be signed in to change notification settings - Fork 0
/
친구네트워크.java
70 lines (60 loc) · 2.12 KB
/
친구네트워크.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
// BOJ - 친구 네트워크(4195번)
// Union Find
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
public static int f, n;
public static int INF = (int)1e9;
public static int[] parent;
public static HashMap<String, Integer> number;
public static HashMap<Integer, Integer> cnt;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = null;
f = Integer.parseInt(br.readLine());
for(int test_case=1;test_case<=f;test_case++){
n = Integer.parseInt(br.readLine());
number = new HashMap<>();
cnt = new HashMap<>();
parent = new int[200001];
for(int i=0;i<200001;i++){
parent[i] = i;
}
int num = 0;
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine(), " ");
String a = st.nextToken();
String b = st.nextToken();
if(!number.containsKey(a)){
number.put(a, ++num);
cnt.put(num, 1);
}
if(!number.containsKey(b)){
number.put(b, ++num);
cnt.put(num, 1);
}
int p_a = find_parent(number.get(a));
int p_b = find_parent(number.get(b));
if(p_a != p_b){
sb.append(cnt.get(p_a)+cnt.get(p_b)+"\n");
union_find(p_a, p_b);
} else {
sb.append(cnt.get(p_a)+"\n");
}
}
}
System.out.println(sb.toString());
}
public static int find_parent(int i) {
if(parent[i] == i) return i;
return parent[i] = find_parent(parent[i]);
}
public static void union_find(int a, int b) {
cnt.put(a, cnt.get(a)+cnt.get(b));
parent[b] = a;
}
}