forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
48 lines (40 loc) · 993 Bytes
/
main.cpp
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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/03ac57850f3e4b83b42d8300cccc91fb
#include<bits/stdc++.h>
using namespace std;
constexpr int MAXN = 200005;
int uf[MAXN], siz[MAXN];
int find(int x) {
if(uf[x] < 0) return x;
return uf[x] = find(uf[x]);
}
bool merge(int a, int b) {
a = find(a); b = find(b);
if(a == b)return false;
uf[b] = a;
siz[a] += siz[b];
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while(t--){
for(int i=0;i<MAXN;i++) {
uf[i] = -1;
siz[i] = 1;
}
int n; cin >> n;
unordered_map<string, int> mp;
int idx = 0;
for(int i=0;i<n;i++){
string a, b; cin >> a >> b;
if(mp.count(a) == 0) mp[a] = idx++;
if(mp.count(b) == 0) mp[b] = idx++;
merge(mp[a], mp[b]);
cout << siz[find(mp[a])] << '\n';
}
}
return 0;
}