"POJ 3020" antenna Placement
The maximal independent set problem of binary graphs
' O ' means that the break point requires that all * connections be connected to one or two consecutive lines per route *
The maximum match can satisfy only the longest path that can be formed by only two consecutive connections, and the points that are not connected need to be circled separately.
i.e. N (total * number)-m (maximum match) +m (maximum match)/2
(due to the fact that the maximum match that is obtained by a bidirectional path actually represents the total number of points connected after 22 connections)
The code is as follows:
#include <cstdlib> #include <cstdio> #include <cstring> #include <iostream> using namespace std;
Char map[41][13];
BOOL mp[555][555],vis[555];
int tp,n,link[555];
int dirx[] = {-1, 0, 1, 0};
int diry[] = {0,-1, 0, 1};
int dir[4];
BOOL can (int p) {int i,j;
for (i = 0; i < 4; ++i) {if (P + Dir[i] < 0 && p + dir[i] >= N) continue;
j = p + dir[i];
if (!vis[j] && mp[p][j]) {vis[j] = true;
if (link[j] = = 1 | | can (LINK[J])) {link[j] = p;
return 1;
}}} return 0;
} int main () {int h,t,w,i,j,k,xx,yy,cnt,sum;
scanf ("%d", &t);
while (t--) {memset (Mp,false,sizeof (MP));
TP = 0;
scanf ("%d%d", &h,&w);
for (i = 1; I <= h; ++i) {scanf ("%s", map[i]+1);
} n = h*w;
Dir[0] =-W;
DIR[1] =-1; DIR[2] = W;
DIR[3] = 1;
sum = n; for (i = 1, i <= h; ++i) {for (j = 1; J <= W; ++j,++tp) {if (map[i][j
] = = ' O ') {sum--;
Continue
} for (k = 0; k < 4; ++k) {xx = i + dirx[k];
yy = j + Diry[k]; if (TP + dir[k] >= 0 && TP + dir[k] < n) {if (map[xx][yy] = = ' * '
) Mp[tp][tp+dir[k]] = true;
}}}} memset (Link,-1,sizeof (link));
CNT = 0;
for (i = 0; i < n; ++i) {memset (vis,0,sizeof (VIS));
if (Can (i)) cnt++;
} printf ("%d\n", SUM-CNT+CNT/2);
} return 0;
}