找回密码
 中文实名注册
搜索
查看: 369|回复: 0

C09第9章二进制枚举子集

[复制链接]

731

主题

577

回帖

2万

积分

管理员

积分
24978
发表于 2025-8-28 08:15:43 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 110;
int a[MAXN] = {0};
int main() {
	freopen("toy.in", "r", stdin);
	freopen("toy.out", "w", stdout);
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 0;i < n; ++i) {
		int tp;
		cin >> tp;
		int tmp;
		for (int j = 0;j < tp; ++j) {
			cin >> tmp;
			a[i] = a[i] | (1 << (tmp - 1));
		}
	}
	int ans = 0;
	for (int i = 0;i < (1 << k); ++i) {
		int tot = 0;
		for (int j = 0;j < k; ++j) {
			if ((1 << j) & i) {
				tot++;
			}
		}
		if (tot > m) {
			continue;
		}
		tot = 0;
		for (int j = 0;j < n; ++j) {
			if ((a[j] & i) == a[j]) {
				tot++;
			}
		}
		ans = max(ans, tot);
	}
	cout << ans << endl;
	return 0;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表