[专题训练1]二分图匹配
匈牙利算法,除了二分图多重匹配外在二分图匹配中都可以使用。
难点在建图与判断二分图。
判断二分图时使用BFS染色法。(还不熟)
定理:一张图是二分图,当且仅当图中不存在奇环(长度为奇数的环)。
最小路径覆盖=顶点个数-最大匹配数
最小点覆盖 = 最大匹配数
进度:
1.Courses HDU1083 (比较裸的完全匹配)
题目链接: 题目在此
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 305;
int m, n, sum;
int line[MAX][MAX], course[MAX], used[MAX];
int find(int x) {
for (int i = 1; i <= n; i++) {//遍历所有被选者
if (line[x][i] && !used[i]) {
used[i] = 1;
if (course[i] == 0 || find(course[i])) {//名花无主或者可以换别人
course[i] = x;
return 1;
}
}
}
return 0;
}
int main() {
//m学生n课程
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
int cnt = 0;
while (t--) {
memset(line, 0, sizeof(line));
memset(course, 0, sizeof(course));
memset(used, 0, sizeof(used));
cin >> m >> n;
for (int i = 1; i <= m; i++) {
cin >> cnt;
for (int j = 1; j <= cnt; j++) {
int c;
cin >> c;
line[i][c] = 1;//i学生可以参加c课
}
}
sum = 0;
for (int i = 1; i <= n; i++) {
memset(used, 0, sizeof(used));
if (find(i))
sum++;
}
puts(sum == m ? "YES" : "NO");//最大匹配数等于课程数
}
return 0;
}
2.The Accomodation of Students HDU2444(二分图判定+匹配)
题目在此
注意点:
①用染色法判定二分图(这部分代码是参考着题解写的…还需要继续琢磨)
②无向图建双边,最后结果除以2
代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#define MS 205
using namespace std;
int n, m,u, v, flag;
int mp[MS][MS];
int col[MS];
const int MAX = 205;
int used[MAX], line[MAX][MAX], linker[MAX];
void dfs(int x) {
if (flag) return;
for (int i = 1; i <= n; i++) {
if (mp[x][i]) {
if (col[i] == col[x]) {
flag = 1;
return;
}
else if (col[i] == -1) {
col[i] = !col[x];
dfs(i);
}
}
}
}
int find(int x) {
for (int i = 1; i <= n; i++) {
if (line[x][i] && !used[i]) {
used[i] = 1;
if (!linker[i] || find(linker[i])) {
linker[i] = x;
return 1;
}
}
}
return 0;
}
void solve() {
memset(linker, 0, sizeof(linker));
int sum = 0;
for (int i = 1; i <= n; i++) {
memset(used, 0, sizeof(used));
if (find(i))
sum++;
}
cout << sum/2 << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n >> m) {
memset(mp, 0, sizeof mp);
memset(col, -1, sizeof col);
flag = 0;
memset(line, 0, sizeof(line));
for (int i = 1; i <= m; i++) {
cin >> u >> v;
mp[u][v] = mp[v][u] = 1;
line[u][v] = line[v][u] = 1;
}
for (int i = 1; i <= n; i++) {//用于判断是否为二分图
if (col[i] == -1) {
col[i] = 0;
dfs(i);
}
}
if (flag) {
cout << "No" << endl;
}
else {
solve();
}
}
return 0;
}
3.swap HDU2819 (如果不告诉我是二分图匹配我可能想不到)
题目在此
只进行行变换或者只进行列变换都可以(然而我已经懵了,不知道自己在行变换还是列变换)
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 105;
int a[MAX][MAX];
int line[MAX][MAX], linker[MAX], used[MAX];
int sum = 0,n;
int x[MAX*MAX], y[MAX*MAX];
int find(int x) {
for (int i = 1; i <= n; i++) {
if (line[x][i] && !used[i]) {
used[i] = 1;
if (linker[i] == 0 || find(linker[i])) {
linker[i] = x;
return 1;
}
}
}
return 0;
}
void bgm() {
sum = 0;
memset(linker, 0, sizeof(linker));
for (int i = 1; i <= n; i++) {
memset(used, 0, sizeof(used));
if (find(i))
sum++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (cin >> n) {
memset(a, 0, sizeof(a));
memset(line, 0, sizeof(line));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
line[i][j] = a[i][j];
}
}
bgm();
if (sum != n) {
cout << -1 << endl;
}
else {
int num = 0;//交换次数
for (int i = 1; i <= n; i++) {//遍历所有列
if (i == linker[i])
continue;
for (int j = 1; j <= n; j++) {
if (i == linker[j]) {
x[++num] = i;
y[num] = j;
swap(linker[i], linker[j]);
break;
}
}
}
cout << num << endl;
for (int i = 1; i <= num; i++) {
cout << "C" << " " << x[i] << " " << y[i] << endl;
}
}
}
return 0;
}
4.Air Raid HDU1151(最小路径覆盖)
题目在此
最小路径覆盖 = 顶点个数 - 最大匹配数
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int MAX = 1505;
int a[MAX][MAX];
int line[MAX][MAX], linker[MAX], used[MAX];
int sum, n, m;
int find(int x) {
for (int i = 1; i <= n; i++) {
if (line[x][i] && !used[i]) {
used[i] = 1;
if (linker[i] == 0 || find(linker[i])) {
linker[i] = x;
return 1;
}
}
}
return 0;
}
void bgm() {
sum = 0;
for (int i = 1; i <= n; i++) {
memset(used, 0, sizeof(used));
if (find(i))
sum++;
}
}
int main() {
int T;
cin >> T;
while (T--) {
memset(linker, 0, sizeof(linker));
memset(line, 0, sizeof(line));
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
line[u][v] = 1;
}
bgm();
cout << n-sum << endl;
}
return 0;
}