LightOJ-1149 Factors and Multiples题解(最大独立集)

Description

传送门: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

1
2
3
4
5
2
4 2 3 4 5
4 6 7 8 9
3 100 200 300
1 150

Sample Output

1
2
Case 1: 3
Case 2: 0

题目大意

题目的意思就是给出两个集合A和B,A集合有n个元素,B集合有m个元素,现在让你在A中删除k1的元素,B中删除k2个元素,使得对于A中任意元素,B中没有该元素的倍数,求最小的k1+k2。

思路

对于这个题,我们可以考虑将集合A和集合B中成倍数关系之间的元素连一条线,题目的要求我们可以转化为减去尽可能少的点,使得剩下的点组成的点集的任意两点之间没有边,并且这个点集数量尽可能的大,这个点集很明显就应该是最大独立集,那么答案就是总点数减去最大独立集点数,其实答案就是最大匹配,我们只需要在A和B中符合条件的两个点之间连线,然后求出最大匹配就行了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/************************************************************
> File Name: Factors_and_Multiples.cpp
> Author: TSwiftie
> Mail: 2224273204@qq.com
> Created Time: Wed 09 Oct 2019 10:34:22 PM CST
************************************************************/

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iomanip>
//#include <unordered_map>
#define lowbit(x) (x&-x)
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e4+5;
const int MAXM = 2e5+5;
const int MOD = 1e9+7;
const double PI = acos(-1.0);
const double EXP = 1e-8;
struct Edge{
int to,next;
}edge[MAXN];
int head[MAXN],tot;
void add_edge(int u,int v){
edge[++tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
}
void init(){
tot = 0;
memset(head,0,sizeof head);
}
int linker[MAXN];
bool vis[MAXN];
int uN;
bool dfs(int u){
for(int i = head[u];i;i = edge[i].next){
int v = edge[i].to;
if(!vis[v]){
vis[v] = true;
if(linker[v] == -1 || dfs(linker[v])){
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary(){
int res = 0;
memset(linker,-1,sizeof linker);
for(int u = 1;u <= uN;u++){
memset(vis,false,sizeof vis);
if(dfs(u))
res++;
}
return res;
}
int a[MAXN],b[MAXN];
int main(void){
int t;
scanf("%d",&t);
for(int cas = 1;cas <= t;cas++){
init();
int la,lb;
scanf("%d",&la);
for(int i = 1;i <= la;i++)
scanf("%d",a+i);
scanf("%d",&lb);
for(int i = 1;i <= lb;i++)
scanf("%d",b+i);
for(int i = 1;i <= la;i++)
for(int j = 1;j <= lb;j++)
if(b[j]%a[i]==0)
add_edge(i,j+la);
uN = la+lb;
printf("Case %d: %d\n",cas,hungary());
}
return 0;
}

本文标题:LightOJ-1149 Factors and Multiples题解(最大独立集)

文章作者:TSwifite

发布时间:2019年10月09日 - 23:10

最后更新:2019年10月14日 - 00:10

原始链接:http://tswiftie.com/LightOJ-1149-Factors-and-Multiples题解-最大独立集/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%