Source: HDU 1281 board games
Some points cannot be attacked when they can be put in the car. Find out which points must be put in the car and cannot get the largest match.
Ideas: each vertex in the row-column matching matrix first finds the largest matching ans for each edge of the Bipartite Graph. If it is time-consuming to remove a vertex and then re-find the largest matching, the first bipartite matching graph is saved.
Then the key points must be the matching edge enumeration to remove the grid point (that is, to remove a matched edge). If it can still be matched, the grid point is not the key point. Instead, do not repeat the maximum match each time.
#include
#include
#include
using namespace std;const int maxn = 110;const int maxm = 10010;bool vis[maxn];int y[maxn];int n, m;int a[maxn][maxn];int b[maxn];bool dfs(int u){ for(int i = 0; i < m; i++) { int v = i; if(vis[v] || !a[u][i]) continue; vis[v] = true; if(y[v] == -1 || dfs(y[v])) { y[v] = u; return true; } } return false;}int match(){ int ans = 0; memset(y, -1, sizeof(y)); for(int i = 0; i < n; i++) { memset(vis, false, sizeof(vis)); if(dfs(i)) ans++; } return ans;}int main(){ int cas = 1; int T; int k; //scanf("%d", &T); while(scanf("%d %d %d", &n, &m, &k) != EOF) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); while(k--) { int u, v; scanf("%d %d", &u, &v); u--; v--; a[u][v] = 1; } int ans = match(); int sum = 0; for(int i = 0; i < m; i++) { memset(b, 0, sizeof(b)); for(int j = 0; j < m; j++) if(y[j] != -1) b[y[j]] = 1; if(y[i] == -1) continue; int temp = y[i]; y[i] = -1; a[temp][i] = 0; b[temp] = 0; int flag = 0; for(int j = 0; j < n; j++) { memset(vis, 0, sizeof(vis)); if(!b[j] && dfs(j)) flag = 1; if(flag) break; } if(!flag) { sum++; y[i] = temp; } a[temp][i] = 1; } printf("Board %d have %d important blanks for %d chessmen.\n", cas++, sum, ans); } return 0;}