-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva_11503 _Virtual_Friends.cpp
More file actions
78 lines (63 loc) · 1.72 KB
/
uva_11503 _Virtual_Friends.cpp
File metadata and controls
78 lines (63 loc) · 1.72 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
72
73
74
75
76
77
78
#include<bits/stdc++.h>
#define pb(n) push_back(n)
#define bug cout<<"bug";
#define all(c) c.begin(),c.end()
#define fr(i,n) for(i=0;i<n;i++)
#define sf(n) scanf("%d",&n)
#define mst(n) memset(n,0,sizeof(n))
#define nl printf("\n")
#define lli long long int
#define tr(container,it) \
for (auto it = container.begin(); it != container.end() ; ++it)
#define INF INT_MAX
#define MOD 1000000007
#define mp(i,j) make_pair(i,j)
#define mstn(n) memset(n,-1,sizeof(n))
#define infile freopen("in.txt","r",stdin)
#define outfile freopen("out.txt","w",stdout)
#define pr(a) cout<<cout<<a.a.first<<" "<< a.a.second<<endl
#define V 5
using namespace std;
int parent[200001];
int chain[200001];
int parent_find(int child)
{
if(parent[child]==child) return child;
parent[child]=parent_find(parent[child]);
return parent[child];
}
void init(int c)
{
for(int i=0; i<c; i++)
{
parent[i]=i, chain[i]=1;
}
}
int main()
{
int test;
sf(test);
while(test--)
{
int relation;
sf(relation);
init(relation*2);
int assin=0;
map<string,int> imap;
for(int i=0; i<relation; i++)
{
char str1[35],str2[35];
scanf("%s %s",str1,str2);
if(imap.find(str1)==imap.end()) imap[str1]=assin++;
if(imap.find(str2)==imap.end()) imap[str2]=assin++;
int par1=parent_find(imap[str1]);
int par2=parent_find(imap[str2]);
if(par1!=par2)
{
parent[par2]=parent[imap[str2]]= par1;
chain[par1]+=chain[par2];
}
printf("%d\n",chain[par1]);
}
}
}