forked from taraqr9/UVA-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva 11503(DSU).cpp
60 lines (52 loc) · 986 Bytes
/
uva 11503(DSU).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
#include<bits/stdc++.h>
using namespace std;
int parent[200000];
int Size[200000];
void makeset(int x)
{
parent[x]=x;
Size[x]=1;
}
int Find(int x)
{
while(x!=parent[x]){
x = parent[x];
}
return x;
}
void Union_by_size(int x,int y)
{
int r = Find(x);
int s = Find(y);
if(r==s) return;
else if(Size[r]>Size[s]){
parent[s] = r;
Size[r]+=Size[s];
}
else{
parent[r] = s;
Size[s]+= Size[r];
}
}
int main()
{
int n,i,j,t;
cin>>t;
while(t--){
cin>>n;
for(i=1;i<=2*n;i++) makeset(i);
map<string,int>mp;
int as=1;
for(i=1;i<=n;i++){
string a,b;
cin>>a;
cin>>b;
if(mp.find(a)==mp.end()) mp[a] = as++;
if(mp.find(b)==mp.end()) mp[b] = as++;
Union_by_size(mp[a],mp[b]);
int cnt = Find(mp[a]);
printf("%d\n",Size[cnt]);
}
}
return 0;
}