ZOJ 1084 贪心 给顶点染色,相邻顶点不能染同样的颜色 算法有问题不能保证是最优解但能A

贴代码:

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAXN 30
 4 int n;
 5 bool edge[MAXN][MAXN];
 6 int color()
 7 {
 8     int vet[MAXN];
 9     int c[MAXN];
10     int i,j;
11     memset(vet,0xff,sizeof(vet));
12     for( i=0; i<n; ++i)
13     {
14         memset(c,0,sizeof(c));
15         for( j=0; j<n; ++j)
16         {
17             if(edge[i][j] && vet[j] != -1)
18                 c[vet[j]] = 1;
19         }
20         for(j=0; j<n; ++j)
21             if(!c[j]) break;
22         vet[i] = j;
23     }
24     int max = -1;
25     for(int i=0; i<n; ++i)
26         if(vet[i] > max) max = vet[i];
27     return max+1;
28 }
29 int main()
30 {
31     freopen("in.cpp","r",stdin);
32     while(~scanf("%d",&n))
33     {
34         if(n==0) break;
35         memset(edge,0,sizeof(edge));
36         char a[34];
37         for(int i=0; i<n; ++i)
38         {
39             scanf("%s",a);
40             int len=strlen(a);
41             int u = a[0]-'A';
42             for(int j=2; j<len; ++j)
43             {
44                 int v = a[j]-'A';
45                 edge[u][v] = edge[v][u] = 1;
46             }
47         }
48         int p = color();
49         if(p == 1)
50             printf("%d channel",p);
51         else
52             printf("%d channels",p);
53         printf(" needed.\n");
54     }
55     return 0;
56 }

 

posted on 2013-04-10 23:27  allh123  阅读(243)  评论(0编辑  收藏  举报

导航