题目大意:给你两个集合,要求你从两个集合中剔除K个数,使得集合B的任意数i,在集合A中,不存在i = j * k(k为整数),且剔除掉的数要的和要达到最小,问最小的K
解题思路:剔除掉的数的和要最小,其实是没什么卵用的,在K最小的情况下,已经存在最小的情况了,要不然,K就要比最小值大
先处理出B集合中能被A整除的,连边,接着进行二分图匹配,要剔除的数就是二分图的最小点覆盖了
#include <cstdio>
#include <cstring>
const int N = 110;
int g[N][N], x[N], y[N], left[N];
bool vis[N];
int n, m, cas = 1;
void init() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &x[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++)
scanf("%d", &y[i]);
memset(g, 0, sizeof(g));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (y[j] == 0) {
g[i][j] = g[j][i] = 1;
continue;
}
if (x[i] == 0 || x[i] > y[j]) continue;
if (y[j] % x[i] == 0) g[i][j] = 1;
}
}
bool dfs(int u) {
for (int i = 1; i <= m; i++) {
if (g[u][i] && !vis[i]) {
vis[i] = true;
if (!left[i] || dfs(left[i])) {
left[i] = u;
return true;
}
}
}
return false;
}
void solve() {
int ans = 0;
memset(left, 0, sizeof(left));
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) ans++;
}
printf("Case %d: %d\n", cas++, ans);
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}