-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4195.cpp
More file actions
58 lines (51 loc) · 872 Bytes
/
4195.cpp
File metadata and controls
58 lines (51 loc) · 872 Bytes
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int arr[200001];
int find(int a) {
if (arr[a] < 0)return a;
return arr[a] = find(arr[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)return;
if (a < b) {
arr[a] += arr[b];
arr[b] = a;
}
else {
arr[b] += arr[a];
arr[a] = b;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
map<string, int>ma;
int cnt = 1;
fill(&arr[0], &arr[200001], -1);
int F;
cin >> F;
for (int i = 0; i < F; i++) {
string a, b;
cin >> a >> b;
if (ma[a] == 0) {
ma[a] = cnt;
cnt++;
}
if (ma[b] == 0) {
ma[b] = cnt;
cnt++;
}
merge(ma[a], ma[b]);
cout << -arr[find(ma[a])] << '\n';
}
}
}