LightOJ--1149--Factors and Multiples(二分图好题)
| Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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 {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
Source
给了两个集合A,B,分别有n,m个数,从A取k1个数,B取k2个数,使得b[ j ]%a[ i ]==0的情况不存在
刚开始以为可以暴力的,但是后来发现暴力真的是挺麻烦,把图画出来之后会发现,其实就是最小点覆盖,二分图性质:最小点覆盖=最大匹配,匈牙利算法跑一次
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>map[200];
int used[200],pipei[200],a[200],b[200];
int n,m;
int find(int x)
{ for(int i=0;i<map[x].size();i++)
{
int y=map[x][i];
if(!used[y])
{
used[y]=1;
if(pipei[y]==-1||find(pipei[y]))
{
pipei[y]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int t,k=1;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(pipei,-1,sizeof(pipei));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
map[i].clear();
}
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(b[j]%a[i]==0)
{
map[i].push_back(j);
}
}
}
int sum=0;
for(int i=0;i<n;i++)
{
memset(used,0,sizeof(used));
sum+=find(i);
}
printf("Case %d: %d\n",k++,sum);
}
return 0;
}
LightOJ--1149--Factors and Multiples(二分图好题)的更多相关文章
- light oj 1149 Factors and Multiples(二分匹配)
LightOJ1149 :Factors and Multiples 时间限制:2000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 You w ...
- 【二分图裸题】poj1325机器调度
题目大意:有两个机器A和B,A机器有n个模式,B机器有m个模式,两个机器最初在0模式 然后有k个作业,每个作业有三个参数i,a,b i代表作业编号,a和b代表第i作业要么在A机器的a模式下完成[或者] ...
- HDU - 1054 Strategic Game (二分图匹配模板题)
二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...
- POJ 3041 Asteroids(二分图模板题)
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...
- Factors and Multiples
Factors and Multiples PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...
- (LightOJ 1149) Factors and Multiples
题目链接:http://lightoj.com/volume_showproblem.php?problem=1149 Description You will be given two sets o ...
- hdu1083二分图匹配模板题
onsider a group of N students and P courses. Each student visits zero, one or more than one courses. ...
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...
- lightoj 1148 Mad Counting(数学水题)
lightoj 1148 Mad Counting 链接:http://lightoj.com/volume_showproblem.php?problem=1148 题意:民意调查,每一名公民都有盟 ...
随机推荐
- [原创]PM2使nodejs应用随系统启动
1.安装 npm install pm2 -gd 2.启动 pm2 start ./bin/www 3.pm2 save 4.pm2 startup 5.pm2 save 注:上面几个步骤有几个坑: ...
- WebGL画点程序v3
本文程序实现画一个点的任务,如下图.其中,点的颜色由Javascript传到片元着色器程序中. 整个程序包含两个文件,分别是: 1. HelloPoint3.html <!DOCTYPE HTM ...
- centos7下安装python3 解决openssl等一系列问题
最近折腾了下centos7,发现按正常方法安装python3.7后面会出现各种操蛋的问题. 主要的问题有三个,openssl版本过低,'_ctypes'缺失,以及安装后sqlite3缺失.下面我会贴出 ...
- 用批处理实现垃圾文件清除/自动关机/清除copy病毒
晚上睡觉之前为了下emule经常使用命令shutdown,最近受一个小程序影响想做个自动关机的批处理文件免的麻烦!网上有高手做了个,不过运行时出 现一个绑定错误,at也不能执行,所以后来自己做了简化版 ...
- day35-2 类的三大特性---多态,以及菱形继承问题
目录 菱形继承问题 经典类 新式类 菱形继承 大招 多态与多态性 多态 多态性 多态在Python中的体现 鸭子类型(重要) 结论 菱形继承问题 经典类 没有继承object类的就是经典类,只有Pyt ...
- eas之如何获取当前用户
在UI扩展类中,获取当前用户可通过工具类SysContext如:SysContext.getSysContext().getCurrentUserInfo()在ControlerBean的扩展类中,获 ...
- 【剑指Offer】27、字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. ...
- js中call、apply、bind的区别
var Person = { name : 'alice', say : function(txt1,txt2) { console.info(txt1+txt2); console.info(thi ...
- Linux思维导图之rpm、yum、编译
yum安装失败: 1.yum client 路径指向不正确:2.yum server 缓存未清理(yum clean all;yum makecache):3.网络不连通
- 2019-04-16 sql tran and try catch :
begin try begin tran tran_addresource -- 标记事务的开始 delete rp insert into Cube.ResourcePool(ResourceTyp ...