(LightOJ 1149) Factors and Multiples

时间:2021-09-12 08:48:20

题目链接:http://lightoj.com/volume_showproblem.php?problem=1149

Description
You will be given two sets of integers. Let's call them set A and set B. Set A contains n elements and set B contains m elements. You have to remove k1 elements from set A and k2 elements from set B so that of the remaining values no integer in set B is a multiple of any integer in set A. k1 should be in the range [0, n] and k2 in the range [0, m]. You have to find the value of (k1 + k2) such that (k1 + k2) is as low as possible. P is a multiple of Q if there is some integer K such that P = K * Q. Suppose set A is {, , , } and set B is {, , , }. By removing and from A and from B, we get the sets {, } and {, , }. Here none of the integers , or is a multiple of or . So for this case the answer is (two from set A and one from set B). Input
Input starts with an integer T (≤ ), denoting the number of test cases. The first line of each case starts with an integer n followed by n positive integers. The second line starts with m followed by m positive integers. Both n and m will be in the range [, ]. Each element of the two sets will fit in a bit signed integer. Output
For each case of input, print the case number and the result. Sample Input Sample Output
Case :
Case :

方法:二分匹配,求最大匹配数

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std; #define met(a,b) memset(a,b,sizeof(a))
#define ll long long
#define N 505
int Map[N][N],vis[N],used[N];
int a[N],b[N];
int n,m;
int han(int u)
{
for(int i=;i<=m;i++)
{
if(!vis[i] && Map[u][i])
{
vis[i]=;
if(!used[i] || han(used[i]))
{
used[i]=u;
return ;
}
}
}
return ;
}
int main()
{
int t,con=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);met(Map,);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(b[j]%a[i]==)
Map[i][j]=;
}
met(used,);int sum=;
for(int i=;i<=n;i++)
{
met(vis,);
sum+=han(i);
} printf("Case %d: %d\n",con++,sum);
}
return ;
}