forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualfriends.cpp
70 lines (54 loc) · 1.08 KB
/
virtualfriends.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 <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int find(vector<int>& d, int a) {
if(d[a] < 0) {
return a;
}
return d[a] = find(d, d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) return;
d[b] += d[a];
d[a] = b;
}
int size(vector<int>& d, int a) {
a = find(d, a);
return -d[a];
}
void solve() {
int n;
cin >> n;
vector<int> d;
map<string,int> idx;
for(int i = 0; i < n; i++) {
string s1, s2;
cin >> s1 >> s2;
if(idx.count(s1) == 0) {
idx[s1] = d.size();
d.push_back(-1);
}
if(idx.count(s2) == 0) {
idx[s2] = d.size();
d.push_back(-1);
}
int v1 = idx[s1];
int v2 = idx[s2];
join(d, v1, v2);
cout << size(d, v1) << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int cases;
cin >> cases;
while(cases--) {
solve();
}
return 0;
}