Factors and Multiples(二分图匹配)LightOJ - 1149

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 m will 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

2

4 2 3 4 5

4 6 7 8 9

3 100 200 300

1 150

Sample Output

Case 1: 3

Case 2: 0


求出其最大匹配即可。

#include<iostream>
#include<cstring>
using namespace std;
int n,m,linker[105],vis[105];
int g[105][105];
bool dfs(int x)
{
	for(int i=0;i<n;i++)
	{
		if(!vis[i]&&g[x][i])
		{
			vis[i]=1;
			if(linker[i]==-1||dfs(linker[i]))
			{
				linker[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int t;
	cin>>t;
	for(int temp=1;temp<=t;temp++)
	{
		memset(g,0,sizeof(g));
		int cnt=0;
		long a[105],b[105];
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>a[i];
		cin>>m;
		for(int i=0;i<m;i++)
			cin>>b[i];
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(b[i]%a[j]==0)
					g[i][j]=1;
			}
		}
		memset(linker,-1,sizeof(linker));
		for(int i=0;i<m;i++)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i))
				cnt++;
		}
		cout<<"Case "<<temp<<": "<<cnt<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没想好叫什么名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值