-
Notifications
You must be signed in to change notification settings - Fork 0
/
10685.cpp
82 lines (64 loc) · 1.43 KB
/
10685.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
/**
* Problem : 10685 - Nature
* Verdict : Accepted.
* Time : 490 ms.
* Write : Mehadi Hasan Menon.
* Date : 29.12.16.
**/
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
unordered_map <string, string> rep;
unordered_map <string, int> number_of_creature;
int max_elimentary_chain = 1;
string find_rep(string s)
{
if(rep[s] == s) {
return s;
}
rep[s] = find_rep(rep[s]);
return rep[s];
}
int union_ab(string a, string b)
{
string u = find_rep(a); string v = find_rep(b);
if(u != v)
{
rep[u] = v;
number_of_creature[v] = number_of_creature[v] + number_of_creature[u];
max_elimentary_chain = max(max_elimentary_chain, number_of_creature[v]);
}
}
int main()
{
freopen("input.txt", "r+", stdin);
int c, r;
string s1, s2;
while(cin >> c >> r)
{
if(c == 0 && r == 0) {
break;
}
// make initial set
for(int i = 0; i < c; i++)
{
cin >> s1;
rep[s1] = s1;
number_of_creature[s1] = 1;
}
for(int i = 0; i < r; i++)
{
cin >> s1 >> s2;
union_ab(s1, s2);
}
cout << max_elimentary_chain << endl;
max_elimentary_chain = 1;
for(int i = 0; i < c; i++)
{
rep.clear();
number_of_creature.clear();
}
}
return 0;
}