二分图的最小点集覆盖

学习链接:https://blog.csdn.net/dark_scope/article/details/8880547
这篇比较简单易懂

学习链接:https://www.cnblogs.com/wangjunyan/p/5563154.html
这篇更详细一些

学习链接:https://www.cnblogs.com/shenben/p/5573788.html

例题:https://vjudge.net/problem/POJ-3041

#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 505;

int match[maxn];
int G[maxn][maxn];
int vis[maxn];
int n, k;
bool dfs(int u) {
	for (int i = 1;i <= n;i++) {
		if (G[u][i] && !vis[i]) {
			vis[i] = 1;
			if (match[i] == 0 || dfs(match[i])) {
				match[i] = u;
				return true;
			}
		}
	}
	return false;
}

int main() {
	cin >> n >> k;
	int a, b;
	for (int i = 0;i < k;i++) {
		cin >> a >> b;
		G[a][b] = 1;
	}
	int cnt = 0;
	for (int i = 1;i <= n;i++) {
		memset(vis, 0, sizeof(vis));
		if (dfs(i))
			cnt++;
	}
	cout << cnt;
}	

例题:https://vjudge.net/problem/HDU-1498

#include<set>
#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 105;
int match[maxn];
int G[maxn][maxn];
int Map[maxn][maxn];
int vis[maxn];
int n, k;
set<int>color;

bool dfs(int u) {
	for (int i = 1;i <= n;i++) {
		if (G[u][i] && !vis[i]) {
			vis[i] = 1;
			if (match[i] == 0 || dfs(match[i])) {
				match[i] = u; //忘了这行WA了好久,哭泣
				return true;
			}
		}
	}
	return false;
}
void init(int color) {
	memset(G, 0, sizeof(G));
	memset(match, 0, sizeof(match));
	for (int i = 1;i <= n;i++) {
		for (int j = 1;j <= n;j++) {
			if (Map[i][j] == color)
				G[i][j] = 1;
		}
	}
}

int main() {
	while (cin >> n >> k && (n || k)) {
		color.clear();
		for (int i = 1;i <= n;i++) {
			for (int j = 1;j <= n;j++) {
				cin >> Map[i][j];
				color.insert(Map[i][j]);
			}
		}
		int Cnt = 0;
		for (auto i = color.begin();i != color.end();i++) {
			init(*i);
			int cnt = 0;
			for (int j = 1;j <= n;j++) {
				memset(vis, 0, sizeof(vis));
				if (dfs(j))
					cnt++;
			}
			if (cnt > k) {
				if (Cnt)
					cout << " ";
				cout << *i;
				Cnt++;
			}
		}
		if (!Cnt)
			cout << "-1";
		cout << endl;
	}
}

关于为什么不能贪心,还没有仔细思考。
有时间思考一下。

码几个链接,有时间在多看一看

http://www.matrix67.com/blog/archives/116
https://blog.csdn.net/qq_34564984/article/details/52778763
https://www.cnblogs.com/jianglangcaijin/p/6035950.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值