描述 Description
Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of his N (1 <= N <= 1,000) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them, then the milk will be too contaminated and will have to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without having to discard the milk.
输入格式 InputFormat
Line 1: Three space-separated integers: N, D, and K * Lines 2..N+1: Line i+1 describes the diseases of cow i with a list of 1 or more space-separated integers. The first integer, d_i, is the count of cow i’s diseases; the next d_i integers enumerate the actual diseases. Of course, the list is empty if d_i is 0. 有 N 头牛, 它们可能患有 D 种病, 现在从这些牛中选出若干头来, 但选出来的牛患病的集合中不过超过 K 种病.
输出格式 OutputFormat
Line 1: M, the maximum number of cows which can be milked.
样例输入 SampleInput
10 5 3
2 2 3
1 3
2 3 4
3 1 2 4
2 2 3
3 2 3 5
3 1 4 5
4 1 3 4 5
3 1 3 5
3 1 3 5
样例输出 SampleOutput
4
来源 Source
Silver
代码 Code
只有十五种病于是状态压缩。bitset 是强大啊。。。
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bitset>
using namespace std;
const int inf=0x7fffffff;
bitset <16> b[1005];
int i,j,t,n,m,d,l,r,k,z,y,x,ans;
inline void read(int &x)
{
x=0;char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
int main()
{
read(n);read(d);read(k);
z=0;
for (i=1;i<=n;i++)
{
b[i].reset();
read(m);
for (j=1;j<=m;j++)
{
read(x);
b[i].set(x-1);
}
}
ans=0;
for (i=0;i<(1<<d);i++)
{
t=0;
bitset <16> s((long)i);
if (s.count()>k) continue;
for (j=1;j<=n;j++) if ((b[j]|s)==s) t++;
ans=max(ans,t);
}
printf("%d\n",ans);
return 0;
}