Question:
In a rectangle, there are n Cities '*'. Now all N cities need to cover wireless networks. If a base station is placed, it can cover at most two adjacent cities.
How many base stations can be placed to ensure that all cities are wireless?
Least path overwrite of undirected bipartite graph = number of vertices-Maximum number of bipartite matching/2
Path overwrite is to find some paths in the graph to overwrite all vertices in the graph, and any vertex has only one path associated with it;
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn = 500 + 5;struct BPM { int n, m; vector<int> G[maxn]; int link[maxn]; bool vis[maxn]; void init(int n) { for(int i=0; i<=n+10; ++i) G[i].clear(); } void AddEdge(int u, int v) { G[u].push_back(v); } bool match(int u) { for(int i=0; i<G[u].size(); ++i) { int v = G[u][i]; if(!vis[v]) { vis[v] = true; if(link[v]==-1 || match(link[v])) { link[v] = u; return true; } } } return false; } int solve(int n) { this->n = n; memset(link, -1, sizeof link ); int ans = 0; for(int u=1; u<=n; ++u) { memset(vis, 0, sizeof vis ); if(match(u)) ans++; } return ans; }};BPM solver;int mtx[maxn][maxn];int dir[4][2]={{-1,0}, {1,0}, {0,1}, {0,-1} };int main(){ int t, h, w; scanf("%d", &t); while(t--) { scanf("%d%d", &h, &w); solver.init(h*w); memset(mtx, 0, sizeof mtx ); int i, j; char str[maxn]; int cnt = 0; for(i=1; i<=h; ++i) { scanf("%s", str); for(j=0; j<w; ++j) { if(str[j]=='*') mtx[i][j+1] = ++cnt; } } for(i=1; i<=h; ++i) for(j=1; j<=w; ++j) if(mtx[i][j]) for(int k=0; k<4; ++k) { int xx = i + dir[k][0]; int yy = j + dir[k][1]; if(mtx[xx][yy]) solver.AddEdge(mtx[i][j], mtx[xx][yy]); } int ans = solver.solve(cnt); printf("%d\n", cnt - ans/2); } return 0;}