/
11503.cc
62 lines (48 loc) · 1.32 KB
/
11503.cc
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
#include <iostream>
#include <vector>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
int main() {
string line;
getline(cin, line);
istringstream ss(line);
int cases;
ss >> cases;
while (cases--) {
getline(cin, line);
ss.clear();
ss.str(line);
int n;
ss >> n;
map<string, int> fr_hash;
vector<int> group;
int index = 0;
for (int i = 0; i < n; ++i) {
getline(cin, line);
ss.clear();
ss.str(line);
string fa, fb;
ss >> fa >> fb;
if (fr_hash.find(fa) == fr_hash.end()) {
fr_hash[fa] = index;
group.push_back(index);
index++;
}
if (fr_hash.find(fb) == fr_hash.end()){
fr_hash[fb] = index;
group.push_back(index);
index++;
}
int neu = min(group[fr_hash[fa]], group[fr_hash[fb]]);
int old = max(group[fr_hash[fa]], group[fr_hash[fb]]);
int friends = 0;
for (int i = 0; i < group.size(); ++i) {
if (group[i] == old) group[i] = neu;
if (group[i] == neu) ++friends;
}
cout << friends << endl;
}
}
}