-
Notifications
You must be signed in to change notification settings - Fork 0
/
11503.cpp
71 lines (56 loc) · 1.33 KB
/
11503.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
/***************************************
* Problem : 11503-Virtual Friends
* Status : Accepted. Time : 1.659ms.
* Author : Mehadi Hasan Menon.
* Date : 07-03-2016.
****************************************/
#include <bits/stdc++.h>
using namespace std;
map <string, string> parent;
map <string, int> cnt;
string find_representative(string s)
{
if(parent[s] == "") // coz first time it will get null:)
return s;
parent[s] = find_representative(parent[s]);
return parent[s];
}
int make_friend(string f1, string f2)
{
string x, y;
x = find_representative(f1);
y = find_representative(f2);
if(x != y) // they are not friend yet.
{
parent[x] = y;
return cnt[y] += cnt[x];
}
return cnt[x]; // or cnt[y] same :)
}
int main()
{
freopen("11503.txt", "r+", stdin);
int tcase, f;
string friend1, friend2;
cin >> tcase;
while(tcase--)
{
cin >> f;
parent.clear();
cnt.clear();
for(int i = 0; i < f; i++)
{
cin >> friend1 >> friend2;
if(cnt[friend1] < 1)
{
cnt[friend1] = 1;
}
if(cnt[friend2] < 1)
{
cnt[friend2] = 1;
}
cout << make_friend(friend1, friend2) << endl;
}
}
return 0;
}