码迷,mamicode.com
首页 > 其他好文 > 详细

Light oj 1149 - Factors and Multiples 【二分图最大匹配】【好题】

时间:2017-06-04 11:44:44      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:pos   ems   pre   element   inpu   center   forum   use   als   

1149 - Factors and Multiples
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 Ak1 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

 


PROBLEM SETTER: SOHEL HAFIZ
SPECIAL THANKS: JANE ALAM JAN


题意:给你N个数组成的序列A和M个数组成的序列B,问你最少去掉A、B序列中多少数——使得A序列中全部数和B序列中全部数都没有整除关系。



乍一看。数论??? 模拟下,发现不像啊。然后画个图发现这不就是匹配吗。。。


思路:对序列B中的每个数B[i]。向A序列中它能整除的全部数建边(注意是按序号建边的)。然后跑匈牙利最大匹配就ok了。




AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#define LL long long
#define MAXN 100+10
#define MAXM 20000+10
#define INF 0x3f3f3f3f
using namespace std;
int a[110], b[110];
int Map[110][110];
bool used[110];
int pipei[110];
int N, M;
int k = 1;
int find(int x)
{
    for(int i = 1; i <= N; i++)
    {
        if(!used[i] && Map[x][i])
        {
            used[i] = true;
            if(pipei[i] == -1 || find(pipei[i]))
            {
                pipei[i] = x;
                return 1;
            }
        }
    }
    return 0;
}
void solve()//求匹配
{
    int ans = 0;
    memset(pipei, -1, sizeof(pipei));
    for(int i = 1; i <= M; i++)
    {
        memset(used, false, sizeof(used));
        ans += find(i);
    }
    printf("Case %d: ", k++);
    printf("%d\n", ans);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(Map, 0, sizeof(Map));
        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 j = 1; j <= N; j++)
            {
                if(b[i] % a[j] == 0)//整除关系建边
                    Map[i][j] = 1;
            }
        }
        solve();
    }
    return 0;
}


Light oj 1149 - Factors and Multiples 【二分图最大匹配】【好题】

标签:pos   ems   pre   element   inpu   center   forum   use   als   

原文地址:http://www.cnblogs.com/ljbguanli/p/6939817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!