-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11503 Virtual Friends
73 lines (66 loc) · 1.22 KB
/
11503 Virtual Friends
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
72
73
#include <bits/stdc++.h>
using namespace std;
int parent[100005];
int arr[100005];
map<string , int> mp;
void set_all()
{
for(int i = 0; i <= 100002; i++)
{
arr[i] = 1;
parent[i] = i;
}
}
int find_p(int n)
{
if(parent[n] == n)
return n;
return parent[n] = find_p(parent[n]);
}
int Uninon (int a , int b)
{
a = find_p(a);
b = find_p(b);
if(a != b)
{
if(arr[a] > arr[b])
{
arr[a] += arr[b];
parent[b] = a;
return arr[a];
}
else
{
arr[b] += arr[a];
parent[a] = b;
return arr[b];
}
}
return arr[a];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int tes;
cin >> tes;
while(tes--)
{
int n;
cin >> n;
string s1, s2;
int in = 0;
set_all();
while(n--)
{
cin >> s1 >> s2;
if(mp.find(s1) == mp.end())
mp[s1] = in++;
if(mp.find(s2) == mp.end())
mp[s2] = in++;
Uninon(mp[s1] , mp[s2]);
cout<<Uninon(mp[s1], mp[s2])<<endl;
}
mp.clear();
}
}