细菌(2009jxd)
Time Limit:1000MS Memory Limit:65536K
Total Submit:3 Accepted:1
Description
近期,农场出现了D (1<= D <=15)种细菌。John 要从他的 N (1<= N <=1,000)头奶牛中尽可能多地选些产奶。但是如果选中的奶牛携带了超过 K (1<= K <=D)种不同细菌,所生产的奶就不合格。请你帮助John 计算出最多可以选择多少头奶牛。
Input
第一行:三个整数 N, D, K
下面N行:第i行表示一头牛所携带的细菌情况。第一个整数 di 表示这头牛所携带的细菌种类数,后面di个整数表示这些细菌的各自种类标号。
Output
只一个数 M,最大可选奶牛数。
Sample Input
6 3 2
0
1 1
1 2
1 3
2 2 1
2 2 1
Sample Output
5
选择:
1,2,3,5,6
只有1#和2#两种细菌
反思:
这题应该是最简单了的,简单一个搜索就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,d,k,ans=0,ni,tmp;
bool bo[100000];
int cow[1001];
int check(int i)
{
int t=0;
while(i>0)
{
int p=i&-i;
t++;
i-=p;
}
return t;
}
int main()
{
freopen("disease.in","r",stdin);
freopen("disease.out","w",stdout);
memset(bo,true,sizeof(bo));
memset(cow,0,sizeof(cow));
scanf("%d%d%d",&n,&d,&k);
for(int i=1;i<=n;++i)
{
scanf("%d",&ni);
for(int j=0;j<ni;++j)
{
scanf("%d",&tmp);
cow[i]+=(1<<(tmp-1));
}
}
for(int i=1;i<=(1<<d);++i)
{
if(check(i)>k)
bo[i]=false;
}
for(int i=1;i<=(1<<d);++i)
{
int jl=0;
if(bo[i])
for(int j=1;j<=n;++j)
{
int tt=(cow[j]&i);
if(tt==cow[j])
++jl;
}
if(jl>ans)
ans=jl;
}
/* for(int i=1;i<=(1<<d);++i)
if(bo[i])
cout<<i<<endl;*/
printf("%d",ans);
return 0;
}