标签:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12983 | Accepted: 6649 |
Description
Input
Output
Sample Input
2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C:ABD D:ABC 0
Sample Output
1 channel needed. 3 channels needed. 4 channels needed.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int n,colour,col[40];
bool mp[30][30],flag;
char s[40];
bool check(int x)
{
for(int i=0;i<26;i++)
{
if(mp[i][x])
{
if(col[i]==col[x])
return false;
}
}
return true;
}
void dfs(int x)
{
if(flag)
return ;
if(x==n)
{
flag=true;
return ;
}
for(int i=1;i<=colour;i++)
{
col[x]=i;
if(check(x))
dfs(x+1);
col[x]=0;
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
flag=false;
memset(mp,0,sizeof(mp));
for(int i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s);
for(int j=2;j<len;j++)
mp[i][s[j]-‘A‘]=mp[s[j]-‘A‘][i]=1;
}
for(colour=1;colour<=4;colour++)
{
dfs(0);
if(flag)
break;
}
if(colour==1)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",colour);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/water-full/p/4528104.html