# D - 棋盘游戏（匈牙利算法）

xiaoxiao2021-02-28  16

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 <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int xx[121], yy[121]; int ma[121][121]; int n, m, k; bool v[121]; int match[121]; bool bfs(int a) { for(int j=1;j<=m;j++) { if(ma[a][j]==1&&v[j]==false) { v[j] = true; if(match[j]==-1||bfs(match[j])) { match[j] = a; return true; } } } return false; } int XYL() { int sum = 0; memset(match, -1, sizeof(match)); for(int i=1;i<=n;i++) { memset(v, false, sizeof(v)); if(bfs(i)==true) sum++; } return sum; } int main() { int t = 0; while(~scanf("%d %d %d", &n, &m, &k)) { t++; memset(ma, 0, sizeof(ma)); for(int i=0;i<k;i++) { scanf("%d %d", &xx[i], &yy[i]); if(!ma[xx[i]][yy[i]]) ma[xx[i]][yy[i]] = 1; } int s = XYL(); int ans = 0; for(int i=0;i<k;i++) { ma[xx[i]][yy[i]] = 0; int ss = XYL(); if(ss<s) ans++; ma[xx[i]][yy[i]] = 1; } printf("Board %d have %d important blanks for %d chessmen.\n", t, ans, s); } return 0; }