forked from IElgohary/Uva-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10685 - Nature.cpp
executable file
·97 lines (75 loc) · 1.55 KB
/
10685 - Nature.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <map>
using namespace std;
int crt[5000];
int rel[5000];
int rnk[5000] = {0};
int findset ( int a)
{
if (crt[a] == a )
return a;
return crt[a] = findset(crt[a]);
}
void unionF(int a , int b)
{
int x = findset(a);
int y = findset(b);
if ( x != y)
{
if ( rnk[x] > rnk[y])
{
crt[y] = x;
rel[x] += rel[y];
}
else if (rnk[x] < rnk [y])
{
crt[x] = y;
rel[y] += rel[x];
}
else
{
crt[y] = x;
rnk[x]++;
rel[x] += rel[y];
}
}
}
int main()
{
while (true)
{
int c , r;
cin >>c >> r;
if ( c == r && r == 0)
break;
map < string , int> mp;
for ( int i = 0 ; i < c ; i++)
crt[i] = i;
for ( int i = 0 ; i < c ; i++)
rel[i] = 1;
for ( int i = 0 ; i < c ; i++)
{
string s;
cin >> s;
mp[s] = i;
}
for ( int i = 0 ; i < r ; i++)
{
string s1 , s2;
cin >> s1 >> s2;
unionF(mp[s1] , mp[s2]);
}
int mx = rel[0];
int cnt= 0;
for ( int i = 1 ; i < c ; i++)
{
if ( mx < rel[i])
{
mx = rel[i];
cnt = i;
}
}
cout << rel[cnt]<< endl;
}
return 0;
}