Description
所以現在Gardon想讓小希來解決一個更難的問題,在保證儘量多的“車”的前提下,棋盤裏有些格子是可以避開的,也就是說,不在這些格子上放車,也可以保證儘量多的“車”被放下。但是某些格子若不放子,就無法保證放盡量多的“車”,這樣的格子被稱做重要點。Gardon想讓小希算出有多少個這樣的重要點,你能解決這個問題麼?
Input
第一行有三個數N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盤的高、寬,以及可以放“車”的格子數目。接下來的K行描述了所有格子的信息:每行兩個數X和Y,表示了這個格子在棋盤中的位置。
Output
Board T have C important blanks for L chessmen.
Sample Input
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
Sample Output
Board 1 have 0 important blanks for 2 chessmen. Board 2 have 3 important blanks for 3 chessmen.
解題思路:
先求出最大匹配,然後枚舉去掉某一個點,如果去掉後,最大匹配少了,那這個點就是重要點。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e4 + 100;
int n, m, k;
bool gra[110][110];
int match[110];
bool vis[110];
//能否找到增廣路徑
bool Augment(int u) {
vis[u] = true;
for (int i = 1; i <= m; ++i) {
if (!gra[u][i]) {
continue;
}
if (!match[i] || !vis[match[i]] && Augment(match[i])) {
match[i] = u;
return true;
}
}
return false;
}
//匈牙利算法:
//用增廣路徑求二分圖最大匹配
int Hungary() {
int ans = 0;
memset(match, 0, sizeof(match));
for (int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof(vis));
if (Augment(i)) {
++ans;
}
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int cas = 0;
while (scanf("%d%d%d", &n, &m, &k) != EOF) {
memset(gra, 0, sizeof(gra));
for (int i = 1, x, y; i <= k; ++i) {
scanf("%d%d", &x, &y);
gra[x][y] = true;
}
int mat = Hungary();
int important = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (gra[i][j]) {
gra[i][j] = false;
if (mat > Hungary()) {
++important;
}
gra[i][j] = true;
}
}
}
printf("Board %d have %d important blanks for %d chessmen.\n", ++cas, important, mat);
}
return 0;
}