-
Notifications
You must be signed in to change notification settings - Fork 0
/
00599ver2.cpp
65 lines (60 loc) · 1.27 KB
/
00599ver2.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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int edge[26];
int num_edge[26];
bool cmp(int a,int b)
{
return a > b;
}
int play(int tail)
{
if(edge[tail] == tail)
return tail;
return play(edge[tail]);
}
int main()
{
int TC,t,tree,acorn;
char s[100],*tok;
gets(s);
TC = atoi(s);
while(TC--)
{
for(int i = 0;i < 26;i++)
edge[i] = -1;
gets(s);
while(s[0] != '*')
{
edge[max(s[1] - 'A',s[3] - 'A')] = min(s[1] - 'A',s[3] - 'A');
gets(s);
}
gets(s);
tok = strtok(s,",");
while(tok)
{
if(edge[tok[0] - 'A'] == -1)
edge[tok[0] - 'A'] = tok[0] - 'A';
tok = strtok(NULL,",");
}
for(int i = 0;i < 26;i++)
num_edge[i] = 0;
for(int i = 0;i < 26;i++)
{
if(edge[i] > -1)
num_edge[play(edge[i])]++;
}
tree = acorn = 0;
for(int i = 0;i < 26;i++)
{
if(num_edge[i] > 1)
tree++;
else if(num_edge[i] == 1)
acorn++;
}
printf("There are %d tree(s) and %d acorn(s).\n",tree,acorn);
}
return 0;
}