2017年8月16日提高组T2 疾病

来源:互联网 发布:王坚 阿里云 编辑:程序博客网 时间:2024/05/16 23:56

Description


现在有n个人,m种病,每个人都患有若干种病。若从这些人中选出若干个人来,但选出来的人的患病集合中不超过k种病,问最多能选出多少个人。

Input


第一行三个整数n,m,k。
接下来n行,每行第一个整数s,表示第i个人患了s种病,接下来s个整数,表示第i个人患的病。

Output


一行一个整数,表示答案。

Sample Input


6 3 2
0
1 1
1 2
1 3
2 2 1
2 2 1

Sample Output


5

Hint


【数据规模与约定】
对于前30%的数据,1<=n<=10,1<=m<=10.
对于前100%的数据,1<=n<=1000,1<=m<=15,1<=k<=m.

Source


BY BPM

Solution


这里m很小,dfs记忆化或者状压dp都可以

Code


#include <stdio.h>#include <string.h>#include <math.h>#include <queue>#include <vector>#include <algorithm>#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)#define erg(i, st) for (int i = ls[st]; i; i = e[i].next)#define fill(x, t) memset(x, t, sizeof(x))#define max(x, y) (x)>(y)?(x):(y)#define min(x, y) (x)<(y)?(x):(y)#define ll long long#define db double#define INF 0x3f3f3f3f#define N 1501#define L 1<<16 | 1int f[2][L], t[N];inline int read(){    int x=0,v=1; char ch=getchar();    while (ch<'0'||ch>'9'){if(ch=='-')v=-1; ch=getchar();}    while (ch<='9'&&ch>='0'){x=x*10+ch-'0'; ch=getchar();}    return x*v;}inline int count(int x){    int cnt = 0;    while (x){        x -= x & (-x);        cnt += 1;    }    return cnt;}int main(void){    int n = read();    int m = read();    int k = read();    rep(i, 1, n){        int c = read();        int s = 0;        while (c --){s |= 1<<read()-1;}        t[i] = s;    }    int lim = (1<<m)-1;    rep(i, 0, n - 1){        rep(j, 0, lim){            f[(i + 1)&1][j] = max(f[i&1][j], f[(i + 1)&1][j]);            f[(i + 1)&1][j|t[i+1]] = max(f[i&1][j] + 1, f[(i + 1)&1][j|t[i+1]]);        }    }    int ans = 0;    rep(i, 0, lim){        if (count(i) <= k){            ans = max(ans, f[n&1][i]);        }    }    printf("%d\n", ans);    return 0;}
原创粉丝点击