hdoj1373 Channel Allocation(极大团)

题意是有若干个接收器,给出每个接收器的相邻接收器。相邻的接收器不能使用同一信号频道。问所需要的信号频道数。

求该无向图的极大团。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #define maxn 30
 5 using namespace std;
 6 int stack[maxn],map[maxn][maxn];
 7 int n,cn,bestn;
 8 void dfs(int x){
 9     if (x>n){
10         bestn=max(cn,bestn);
11         return ;
12     }
13     int flag=1;
14     for (int i=0;i<cn;i++){
15         if (!map[stack[i]][x]){
16             flag=0;
17             break;
18         }
19     }
20     if (flag){
21         stack[cn++]=x;
22         dfs(x+1);
23         cn--;
24     }
25     if (cn+n-x>bestn){
26         dfs(x+1);
27     }
28 }
29 int main(){
30     string S;
31     while (cin >> n && n){
32         memset(map,0,sizeof(map));
33         for (int id=0;id<n;id++){
34             cin >> S;
35             int pre=S[0]-'A';
36             int len=S.length();
37             for (int i=2;i<len;i++){
38                 int res=S[i]-'A';
39                 map[pre][res]=map[res][pre]=1;
40             }
41            }
42         cn=bestn=0;
43         memset(stack,0,sizeof(stack));
44         dfs(0);
45         if (bestn==1) cout << "1 channel needed.\n";
46         else cout << bestn << " channels needed.\n";
47     }
48     return 0;
49 }

 

posted @ 2018-03-06 14:36  Changer-qyz  阅读(157)  评论(0编辑  收藏  举报