forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1129(AC).cpp
75 lines (65 loc) · 1.01 KB
/
POJ1129(AC).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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int a[26][26];
int c[26];
int n;
void dfs(int x)
{
int i;
int tag[27];
if(c[x] > 0){
//This node is already colored.
return;
}
memset(tag, 0, 27 * sizeof(int));
for(i = 0; i < n; ++i){
if(a[x][i] && c[i] > 0){
tag[c[i]] = 1;
}
}
for(i = 1; i <= 26; ++i){
if(!tag[i]){
c[x] = i;
break;
}
}
for(i = 0; i < n; ++i){
if(a[x][i] || !c[i]){
dfs(i);
}
}
}
int main()
{
int i, j;
int x, y;
char str[100];
while(scanf("%d", &n) == 1 && n > 0){
memset(a, 0, 26 * 26 * sizeof(int));
memset(c, 0, 26 * sizeof(int));
for(i = 0; i < n; ++i){
scanf("%s", str);
x = str[0] - 'A';
for(j = 2; str[j]; ++j){
y = str[j] - 'A';
a[x][y] = a[y][x] = 1;
}
}
dfs(0);
j = c[0];
for(i = 1; i < n; ++i){
if(c[i] > j){
j = c[i];
}
}
if(j == 1){
printf("%d channel needed.\n", j);
}else{
printf("%d channels needed.\n", j);
}
}
return 0;
}