-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRNDCIRC.cpp
More file actions
71 lines (64 loc) · 1.18 KB
/
FRNDCIRC.cpp
File metadata and controls
71 lines (64 loc) · 1.18 KB
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
70
71
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int parent[200005], wt[200005];
void initialise(int len) {
for (int i = 0; i <= len; i++) {
parent[i] = i;
wt[i] = 1;
}
}
int root(int i) {
while (i != parent[i]) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void merge (int a, int b) {
if (root(a) != root(b)) {
if (wt[root(a)] > wt[root(b)]) {
wt[root(b)] += wt[root(a)];
parent[root(a)] = root(b);
}
else {
wt[root(a)] += wt[root(b)];
parent[root(b)] = root(a);
}
}
}
void display(int len) {
for (int i = 1; i <= len; ++i) {
cout << parent[i] << " ";
}
cout << endl;
for (int i = 1; i <= len; ++i) {
cout << wt[i] << " ";
}
cout << endl;
}
int main() {
freopen("input.txt", "r", stdin);
int test_cases, users, cnt;
string a, b;
map <string, int> mp;
scanf("%d", &test_cases);
while (test_cases--) {
cnt = 1;
mp.clear();
scanf("%d", &users);
initialise(2*users);
while (users--) {
cin >> a >> b;
if (mp.find(a) == mp.end())
mp[a] = cnt++;
if (mp.find(b) == mp.end())
mp[b] = cnt++;
merge(mp[a], mp[b]);
// display(4);
printf("%d\n", wt[root(mp[a])]);
}
}
return 0;
}