-
Notifications
You must be signed in to change notification settings - Fork 1
/
11503-Virtual Friends.cpp
executable file
·94 lines (75 loc) · 1.55 KB
/
11503-Virtual Friends.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <map>
using namespace std;
int ppl[200000];
int frn[200000] ;
int rnk[200000] = {0};
int findset(int a )
{
if ( ppl[a] == a)
return a;
return ppl[a] = findset(ppl[a]);
}
int unionF(int a , int b)
{
int n;
int x = findset(a);
int y = findset(b);
if ( x != y)
{
if ( rnk[x] > rnk[y])
{
ppl[y] = x;
frn[x]+= frn[y];
n = frn[x];
}
else if ( rnk[y] > rnk[x])
{
ppl[x] = y;
frn[y]+= frn[x];
n = frn[y];
}
else
{
ppl[y] = x;
rnk[x] ++;
frn[x]+= frn[y];
n = frn[x];
}
}
return frn[findset(x)];
}
int main()
{
int t;
cin >> t;
while(t--)
{
map<string, int> mp;
int f;
cin >> f;
int x = 0;
for ( int i = 0 ; i <= 2*f ; i++)
ppl[i] = i;
for ( int i = 0 ; i <= 2*f ; i++)
frn[i] = 1;
while ( f--)
{
string s1;
string s2;
cin >> s1 >> s2;
if ( mp.count(s1) == 0)
{
mp[s1] = x;
x++;
}
if ( mp.count(s2) == 0)
{
mp[s2] = x;
x++;
}
cout<<unionF(mp[s1] , mp[s2])<< endl;
}
}
return 0;
}