学习链接: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