-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path599_the_forest.c
75 lines (63 loc) · 1.91 KB
/
599_the_forest.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
73
74
/*
* Author: Chen Rushan
* E-Mail: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
char G[26][26];
char exist[26], visit[26];
void
DFS(int v)
{
int i = 0;
visit[v] = 1;
for (i = 0; i < 26; i++) {
if (G[v][i] && ! visit[i])
DFS(i);
}
}
int
main(int argc, char **argv)
{
int tc = 0, i = 0, j = 0;
char line[1000];
scanf("%d\n", &tc);
while (tc--) {
memset(G, 0, sizeof(G));
memset(exist, 0, sizeof(exist));
memset(visit, 0, sizeof(visit));
while (1) {
char b = 0, e = 0;
fgets(line, sizeof(line), stdin);
if (line[0] == '*')
break;
sscanf(line, "(%c,%c)", &b, &e);
G[b - 'A'][e - 'A'] = 1;
G[e - 'A'][b - 'A'] = 1;
}
fgets(line, sizeof(line), stdin);
for (i = 0; line[i] != '\n'; i++) {
if (line[i] == ',')
continue;
exist[line[i] - 'A'] = 1;
}
int nt = 0, na = 0;
for (i = 0; i < 26; i++) {
if (! (exist[i] && ! visit[i]))
continue;
for (j = 0; j < 26; j++)
if (G[i][j])
break;
if (j == 26) {
na++;
} else {
nt++;
DFS(i);
}
}
printf("There are %d tree(s) and %d acorn(s).\n", nt, na);
}
return 0;
}