题目:http://www.lightoj.com/volume_showproblem.php?problem=1149
题意:给定两个整数集合a,b,现在从a中抽掉k1个数,从b中抽掉k2个数,使b集合剩余的数字没有a集合剩余数字的倍数,求k1 + k2的最小值
思路:对于集合b中的多个数字(设cnt1个)和集合a中的多个数字(设cnt2个)之间有倍数关系时,若想抽掉的数字最少,那么肯定要抽掉min(cnt1, cnt2),那么我们可以对这些数字连边求最大匹配,最大匹配就是min(cnt1, cnt2),对于多组倍数关系,它们之间的最大匹配互不干扰
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 110;
struct edge
{
int to, next;
}g[N*N];
int match[N], head[N];
bool use[N];
int nx, ny, cnt, cas = 0;
void add_edge(int v, int u)
{
g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;
}
bool dfs(int v)
{
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(use[u] == false)
{
use[u] = true;
if(match[u] == -1 || dfs(match[u]))
{
match[u] = v;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(match, -1, sizeof match);
for(int i = 1; i <= nx; i++)
{
memset(use, 0, sizeof use);
if(dfs(i)) res++;
}
return res;
}
int main()
{
int t, n, m;
int arr[N], brr[N];
scanf("%d", &t);
while(t--)
{
cnt = 0;
memset(head, -1, sizeof head);
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
scanf("%d", &m);
for(int i = 1; i <= m; i++)
scanf("%d", &brr[i]);
for(int i = 1; i <= m; i++) //b集合为左点集,a集合为右点集
for(int j = 1; j <= n; j++)
if(brr[i] % arr[j] == 0)
add_edge(i, j);
nx = m, ny = n;
int res = hungary();
printf("Case %d: %d\n", ++cas, res);
}
return 0;
}