Light oj 1149--Factors and Multiples【二分匹配 && 经典建图】

1149 - Factors and Multiples

PDF (English) Statistics Forum
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 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

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int map[110][110];
int a[110], b[110];
int n, m;
int used[110], link[110];

bool dfs(int x){
    for(int i = 1; i <= n; ++i){
        if(map[x][i] && !used[i]){
            used[i] = 1;
            if(link[i] == -1 || dfs(link[i])){
                link[i] = x;
                return true;
            }
        }
    }
    return false;
}

void hungary(){
    int ans = 0;
    memset(link, -1, sizeof(link));
    for(int j = 1; j <= m; ++j){
        memset(used, 0, sizeof(used));
        if(dfs(j))
            ans++;
    }
    printf("%d\n", ans);
}

int main (){
    int T;
    scanf("%d", &T);
    int k = 1;
    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 i = 1; i <= m; ++i){
            for(int j = 1; j <= n; ++j){
                if(b[i] % a[j] == 0)
                    map[i][j] = 1;
            }
        }
        printf("Case %d: ", k++);
        hungary();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-13 09:19:13

Light oj 1149--Factors and Multiples【二分匹配 && 经典建图】的相关文章

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

1149 - Factors and Multiples PDF (English) Statistics Forum 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 remo

Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子可以共存 阴阳离子都至少需要存在一种 那么可以枚举哪2种离子共存 假设枚举a b 然后找到所有的和a可以共存的阴离子(设为x集合)以及和b共存的阳离子(设为y集合) 现在只需求x集合中和y集合中最多有多少个离子可以共存 这个求最大独立集就行了 枚举所有的a b 取最大值 #include <cstdio> #include

POJ3343&amp;HDU2413Against Mammoths (二分匹配)经典

Against Mammoths Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 316 Accepted Submission(s): 90 Problem Description Back to year 3024, humans finally developed a new technology that enables them t

UVALive 6525 Attacking rooks 二分匹配 经典题

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: 给定n*n的棋盘, 能够在'.'上摆 象棋中的车(X是墙壁) 使得随意两个车都不能互相攻击到 问:最多能摆多少个车. 思路: 二分匹配 1.若没有X.那么做法就是 X点集为行,Y点集为列,对于图上的每一个点所在的行和列(x,y) 建一条边 x->y 2.有了X,那么对于每一个点所在的上方能接触到的X必须

LightOJ 1149 - Factors and Multiples【二分图最大匹配】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1149 代码: #include <iostream> #include <algorithm> #include <set> #include <map> #include <string.h> #include <queue> #include <sstream> #include <stdio

ZOJ 1654--Place the Robots【二分匹配 &amp;amp;&amp;amp; 经典建图】

Place the Robots Time Limit: 5 Seconds      Memory Limit: 32768 KB Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of

ZOJ 3640--Missile【二分查找 &amp;&amp; 最大流dinic &amp;&amp; 经典建图】

Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Every tower has enough missiles, but for each tower only one missile can be launch at the same time. Before the launching, every missile needT1 seconds

SPOJ 13041 BNU 28769 The Black Riders 二分+费用流 建图

题目链接: 题意: 给定n个人 m个逃生洞穴 至少k个人进入逃生洞穴 挖洞时间c 下面n*m的矩阵表示每个人到每个洞需要的时间. 一个洞穴开始只能容纳一个人,可以被拓展一次,即变成可以容纳2个人(一个洞穴只能被拓展一次) 当a进入洞穴后不会开始拓展,直到有一个人b在洞穴门口等,a才会开始拓展空间,拓展的时间的c,c时间后b才能进入洞穴. 问至少k个人进入洞穴的最短时间.(数据保证有解) 我们可以认为一个洞穴里面有2个房间,而第二个房间就相当于某个人跑到了这个洞穴然后这个人自己开始挖,则花费就是到

POJ 2724 奶酪消毒机 二分匹配 建图 比较难想

Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5004   Accepted: 1444 Description Mike is the owner of a cheese factory. He has 2N cheeses and each cheese is given a binary number from 00...0 to 11...1. To keep his chee