Given a grid map of n * n, some attack devices should be placed at 0. The attack scope of one of the attack devices is the surrounding eight "day" areas, it is required that no attack can be performed on each other, and the maximum number of attack devices can be placed
Each two vertex that can attack each other and can be placed connects a bidirectional edge, and then runs the maximum vertex independence set of the bipartite graph.
4 W points n ^ 2, isn't it because the data is too weak or the Hungarian algorithm is too strong?
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define M 210 using namespace std; struct abcd {int to, next ;} table [M * M <3]; int head [M * M], tot; int n, m, ans; int map [M] [M]; int state [M * M], result [M * M], T; const int dx [] = {1, 1, 2}; const int dy [] = {2, -2, 1,-1}; void Add (int x, int y) {table [++ tot]. to = y; table [tot]. next = head [x]; head [x] = tot;} bool Hungary (int x) {int I; for (I = head [x]; I; I = table [I]. next ) {If (state [table [I]. to] = T) continue; state [table [I]. to] = T; if (! Result [table [I]. to] | Hungary (result [table [I]. to]) {result [table [I]. to] = x; return true;} return false;} int main () {int I, j, k; cin> n; for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) {scanf ("% 1d ", & map [I] [j]); if (map [I] [j] = 1) map [I] [j] =-1; elsemap [I] [j] = ++ m;} for (I = 1; I <= n; I ++) for (j = 1; j <= n; j ++) if (~ Map [I] [j]) for (k = 0; k <4; k ++) {int xx = I + dx [k]; int yy = j + dy [k]; if (xx <= 0 | yy <= 0 | xx> n | yy> n) continue; if (map [xx] [yy] =-1) continue; Add (map [I] [j], map [xx] [yy]); add (map [xx] [yy], map [I] [j]);} ans = m <1; for (I = 1; I <= m; I ++) {++ T; if (Hungary (I) -- ans;} cout <(ans> 1) <endl ;}
BZOJ 3175 Tjoi2013 maximum matching of attack device bipartite graph