-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11503.cpp
69 lines (58 loc) · 1.47 KB
/
11503.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int groups[200000], size[200000];
int root(int x)
{
while (x != groups[x])
x = groups[x];
return x;
}
int main(void)
{
char cA[21], cB[21];
string nameA, nameB;
map<string, int> names;
int T, n, gA, gB;
scanf("%d", &T);
while (T-- > 0) {
scanf("%d", &n);
names.clear();
for (int c = 0; c < n; ++c) {
scanf("%s %s", cA, cB);
nameA = string(cA);
auto itA = names.find(nameA);
if (itA == names.end()) {
gA = int(names.size());
groups[gA] = gA;
size[gA] = 1;
names[nameA] = gA;
}
else {
gA = root(itA->second);
}
nameB = string(cB);
auto itB = names.find(nameB);
if (itB == names.end()) {
gB = int(names.size());
groups[gB] = gB;
size[gB] = 1;
names[nameB] = gB;
}
else {
gB = root(itB->second);
}
if (gA != gB) {
if (gA > gB) swap(gA, gB);
groups[gB] = gA;
size[gA] += size[gB];
}
printf("%d\n", size[gA]);
}
}
return 0;
}