EOJ 2069 二分图匹配模板

首先要想到把题目给我们的图建成一个二分图,x集合就是有障碍物的行号,y集合就是有障碍物的列号。如果(x,y)有障碍物,就在i,j之间连一条边。

一条边就是一个障碍物,当我们选中一个点的时候,就把和这个点相连的边都删掉。直到把所有的边都删除掉。所以我们要选择最少的点删除完所有的边。所以答案就是最小覆盖点。因为最小覆盖点等于最大匹配数,所以只要套匈牙利算法的模板求出最大匹配数就行了。

#pragma warning(disable:4996);
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <queue>
#include <new>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;
const double pi = (acos(-1));
const int maxn = 1000;
int link[maxn], g[maxn][maxn];
bool used[maxn];
int nx, ny, n, m;

bool dfs (int u) {
	for (int v = 1; v <= ny; v++) {
		if (!used[v] && g[u][v]) {
			used[v] = true;
			if (link[v] == -1 || dfs (link[v])) {
				link[v] = u;
				return true;
			}
		}
	}
	return false;
}

int maxmatch () {
	int res = 0;
	memset (link, -1, sizeof (link));//link[i]=-1表示i不在匹配中,否则(link[i],i)这条边在匹配中
	for (int i = 1; i <= nx; i++) {
		memset (used, false, sizeof (used));
		if (dfs (i)) res++;
	}
	return res;
}

int main()
{
	//freopen("D:\\Test_in.txt", "r", stdin);
	//freopen("D:\\Test_out.txt", "w", stdout);
	while (~scanf ("%d%d", &n, &m)) {
		nx = ny = n;
		memset (g, 0, sizeof (g));
		for (int i = 0; i < m; i++) {
			int a, b;
			scanf ("%d%d", &a, &b);
			g[a][b] = 1;
		}
		printf ("%d\n", maxmatch ());
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值