-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFRNDCIRC.c
72 lines (72 loc) · 1.12 KB
/
FRNDCIRC.c
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
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000006
vector<pair<string,string > > vt;
map<string,int> mp;
int parent[MAX];
int size[MAX];
int findset(int i)
{
while(i != parent[i])
{
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void unionset(int a,int b)
{
int pa = findset(a);
int pb = findset(b);
if(pa != pb)
{
parent[pa] = pb;
size[pb] += size[pa];
cout<<size[pb]<<endl;
}
else
{
cout<<size[pb]<<endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
vt.clear();
mp.clear();
int n;
int i;
int no = 1;
int u,v;
string stra,strb;
cin>>n;
for(i=0;i<n;++i)
{
cin>>stra>>strb;
vt.push_back(make_pair(stra,strb));
mp[stra]++;
mp[strb]++;
}
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();++it)
{
it->second = no;
no++;
}
for(i=1;i<=mp.size();++i)
{
parent[i] = i;
size[i] = 1;
}
for(i=0;i<vt.size();++i)
{
u = mp[vt[i].first];
v = mp[vt[i].second];
unionset(u,v);
}
}
}