UVa11159 - Factors and Multiples(二分匹配)

You will be given two sets ofintegers. Let�s call them set A and setB.Set Acontains nelements and setB contains m elements. You have to remove k1elements from set A and k2elements from setB so that of the remaining valuesno integer in set B is a multiple of any integer insetA.k1should be in the range [0,n] andk2 in therange [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 integerKsuch 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 fromA and 8from B,we get the sets {4,5} and {6,7,9}.Here none of the integers 6,7 or 9is a multiple of 4 or 5.

 

So for this case the answer is 3(2from set Aand 1from set B).

 

Input

 

The first line of input is aninteger T(T<50) thatdetermine the number of test cases. Each case consists of two lines. The firstline starts withn followed by n integers. The second line startswithmfollowed by m integers. Both n andm will be in therange [1,100].All the elements of the two sets will fit in32 bit signedinteger.

 

Output

 

For each case, output thecase number followed by the answer.

 

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

 题意:给出两个集合A和B,从A,B集合中删除最小的数,使得集合B中没有元素是集合 A元素的倍数

思路:用最大二分匹配,注意0是0的倍数

用匈牙利算法的DFS耗时0.016m

11159 - Factors and Multiples AcceptedC++0.0160.0002772 hours ag

#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 100;

int x[MAXN], y[MAXN];
int g[MAXN][MAXN];
int n, m;
int mx[MAXN], my[MAXN];
bool vis[MAXN];

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }    

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d", &y[i]);
    }
}

bool match(int u)
{
    for (int i = 0; i < m; i++) {
        if (!vis[i] && g[u][i]) {
            vis[i] = true;
            if (my[i] == -1 || match(my[i])) {
                my[i] = u;
                mx[u] = i;
                return true;
            }
        }
    }    

    return false;
}

void solve()
{
    memset(g, 0x00, sizeof(g));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if ((x[i] != 0 && y[j] % x[i] == 0) || (x[i] == 0 && y[j] == 0)) g[i][j] = 1;        
        }
    }

    memset(mx, 0xff, sizeof(mx));
    memset(my, 0xff, sizeof(my));
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (mx[i] == -1) {
            memset(vis, false, sizeof(vis));
            if (match(i)) ans++;
        }
    }

    printf("%d\n", ans);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("/cygdrive/d/OJ/uva_in.txt", "r", stdin);
#endif

    int cas;
    scanf("%d", &cas);
    for (int i = 1; i <= cas; i++) {
        printf("Case %d: ", i);
        input();
        solve();
    }
    return 0;
}

用匈牙利算法的BFS耗时0.012s
11159 - Factors and Multiples AcceptedC++0.0120.00016416 mins ago
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 100;

int x[MAXN], y[MAXN];
int g[MAXN][MAXN];
int n, m;
int mx[MAXN], my[MAXN];

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &x[i]);
    }    

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d", &y[i]);
    }
}

int match()
{
	int pre[MAXN];
	int ans = 0;

	for (int i = 0; i < n; i++) {
		if (mx[i] == -1) {
			for (int j = 0; j < m; j++) pre[j] = -2;
			int q[MAXN];
			int front = 0, rear = 0;
			int cur;

			for (int j = 0; j < m; j++) {
				if (g[i][j]) {
					pre[j] = -1;
					q[rear++] = j;
				}
			}

			while (front < rear) {
				cur = q[front];
				if (my[cur] == -1) break;
				front++;
				for (int j = 0; j < m; j++) {
					if (pre[j] == -2 && g[my[cur]][j]) {
						pre[j] = cur;
						q[rear++] = j;
					}
				}
			}

			if (front >= rear) continue;

			while (pre[cur] > -1) {
				mx[my[pre[cur]]] = cur;
				my[cur] = my[pre[cur]];
				cur = pre[cur];
			}		

			mx[i] = cur; my[cur] = i;	
			ans++;
		}

	}
	return ans;
}

void solve()
{
    memset(g, 0x00, sizeof(g));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if ((x[i] != 0 && y[j] % x[i] == 0) || (x[i] == 0 && y[j] == 0)) g[i][j] = 1;        
        }
    }

    memset(mx, 0xff, sizeof(mx));
    memset(my, 0xff, sizeof(my));
    int ans = match();

    printf("%d\n", ans);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("./uva_in.txt", "r", stdin);
#endif

    int cas;
    scanf("%d", &cas);
    for (int i = 1; i <= cas; i++) {
        printf("Case %d: ", i);
        input();
        solve();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值