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?
Ideas:
What is difficult for a bipartite graph is not how hard its code is, but how it should be created. Like this question, it seems that it has nothing to do with the bipartite graph. But after a slight conversion, we will find that this is a problem with the bipartite graph.
First, we can mark it from the first *, that is, the city. For example:
* ** Marked as: 123
OO * oo4
** O 56o
Then, we will store 123456 in V1 V2, respectively, so for 3, its matching points are 2 and 4, then 32 and 34 can be marked
The rest is the basic bipartite graph idea;
AC code:
# Include <iostream> # include <string. h> using namespace STD; int link [405] [405]; // link int City [45] [15]; // records the city location int vis [405], col [405]; // not explained! Int H, W, V1, V2, M, TOT; int dir [4] [2] = {,-, 0,-, 1 }; int DFS (int x) {for (INT I = 1; I <= V2; I ++) {If (! Vis [I] & link [x] [I]) {vis [I] = 1; if (COL [I] = 0 | DFS (COL [I]) {Col [I] = x; return 1 ;}} return 0 ;} int main () {int N, I, j; CIN> N; while (n --) {m = 0; Tot = 0; memset (link, 0, sizeof (Link); memset (city, 0, sizeof (city); memset (COL, 0, sizeof (COL); CIN> H> W; char C; for (I = 1; I <= H; I ++) for (j = 1; j <= W; j ++) {CIN> C; if (C = '*') {tot + = 1; City [I] [J] = tot;} for (I = 1; I <= h; I ++) for (j = 1; j <= W; j ++) if (City [I] [J]) {for (int K = 0; k <4; k ++) {// find the point that matches the current position in four directions: int x = I + dir [k] [0]; int y = J + dir [k] [1]; If (City [x] [Y]) link [City [I] [J] [City [x] [Y] = 1 ;}} V1 = v2 = tot; // cout <tot <Endl; for (I = 1; I <= V1; I ++) {memset (VIS, 0, sizeof (VIS); If (DFS (I) m ++ ;} // cout <m <Endl; cout <tot-M/2 <Endl; // Why do you want this?} return 0 ;}