Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 {2, 3, 4, 5} and set B is {6, 7, 8, 9}. By removing 2 and 3 from A and 8 from B, we get the sets {4, 5} and {6, 7, 9}. Here none of the integers 6, 7 or 9 is a multiple of 4 or 5.
So for this case the answer is 3 (two from set A and one from set B).
Input
Input starts with an integer T (≤ 50), 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 mwill be in the range [1, 100]. Each element of the two sets will fit in a 32 bit signed integer.
Output
For each case of input, print the case number and the result.
Sample Input | Output for Sample Input |
2 4 2 3 4 5 4 6 7 8 9 3 100 200 300 1 150 | Case 1: 3 Case 2: 0 |
题目大意:
集合A中有N个数,集合B中有M个数,现在需要在A集合中去掉K1个数,在B集合中去掉K2个数,现在需要(K1+K2)尽可能的小,保证剩余部分中,B集合中的任意数都不能整除A集合中的任意数。
思路:
1、和2-SAT很像 ,这种题肯定是找矛盾边.找到矛盾边之后两点连边,那么问题就变成了最小点覆盖问题。
去掉最少的点,使得剩余部分没有连边。
2、那么接下来我们让B集合和A集合进行建图,建好图之后直接跑匈牙利二分匹配算法即可。
最小点覆盖==最大二分匹配数。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m;
int match[10000];
int vis[10000];
vector<int >mp[10000];
int a[10000];
int b[10000];
int find(int u)
{
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
int kase=0;
scanf("%d",&t);
while(t--)
{
memset(match,-1,sizeof(match));
for(int i=1;i<=9980;i++)mp[i].clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if((b[i]%a[j])==0)
{
mp[i].push_back(j);
}
}
}
int output=0;
for(int i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))output++;
}
printf("Case %d: %d\n",++kase,output);
}
}