BZOJ_4554_[Tjoi2016&Heoi2016]Game_Bipartite Matching

BZOJ_4554_[Tjoi2016&Heoi2016]Game_Bipartite Matching

Description

In 2016, Sister Jiayuan fell in love with a game called Bubble Hall. Simply put, this game is to put a number of bombs on a map, see
Whether it can blow up the opponent, or dodge the opponent's bomb. In the process of playing the game, Xiao H thought of such a problem: when a map is given, in this
The maximum number of bombs that can be placed on the map is such that any two bombs do not blow up each other. The range that the bomb can reach is the line and the line where the bomb is located.
Column, the power of the bomb can penetrate soft rock, but not hard rock. Given a grid map of n*m: where * represents the open space, the power of the bomb can be
To penetrate, a bomb can be placed in an open space. The x stands for soft stone, the power of the bomb can penetrate, and the bomb cannot be placed here. # stands for hard rock, fried
The power of the bomb is impenetrable, and bombs cannot be placed here. For example: Given a 1*4 grid map *xx*, only one bomb can be placed on this map
. Given another 1*4 grid map *x#*, this map can place up to two bombs. Now Xiao H randomly gives a grid map of n*m and asks you the most
how many bombs can be placed

Input

The first line inputs two positive integers n, m, n represents the number of rows of the map, and m represents the number of columns of the map. 1≤n, m≤50. Next, enter n lines and m columns of characters, representing the network
grid map. The number of * does not exceed n*m

Output

Output an integer a, indicating the maximum number of bombs that can be placed

Sample Input

```
4 4
#***
*#**
**#*
xxx#
```

Sample Output

5

If there is no hard stone, make a bipartite graph directly by ranks and columns, and connect the rows and columns of points of each non-soft stone to find the maximum matching of the bipartite graph.
Now that there are hard stones, it is equivalent to the middle of each row and column is separated by hard stones, and the divided parts do not affect each other.
So each row and column create new points for these separated parts, and then take them out for bipartite graph matching.
Make sure there is enough space.
 
Code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 55000
#define M 1000050
int n,m;
int vis[N],match[N],head[N],to[M],nxt[M],cnt,tot,hang[60][60],lie[60][60];
char mp [60] [60];
struct node {
    int type,v,l,r;
}a[N];
inline void add(int u,int v) {
    to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
bool dfs(int x) {
    int i;
    for(i=head[x];i;i=nxt[i]) {
        if(!vis[to[i]]) {
            vis [to [i]] = 1;
            if(!match[to[i]]||dfs(match[to[i]])) {
                match[to[i]]=x; return 1;
            }
        }
    }
    return 0;
}
int main() {
    //freopen("game.in","r",stdin);
    //freopen("game.out","w",stdout);
    scanf("%d%d",&n,&m);
    int i,j,lst,k;
    for(i=1;i<=n;i++) {
        scanf("%s",mp[i]+1);
    }
    for(i=1;i<=n;i++) {
        lst=1;
        for(j=1;j<=m;j++) {
            if(mp[i][j]=='#') {
                if(j!=1&&lst<=j-1) {
                    a[++tot]=(node){0,i,lst,j-1};
                    for(k=lst;k<j;k++) hang[i][k]=tot;
                }
                lst=j+1;
            }
        }
        if(lst<=m) {
            a[++tot]=(node){1,i,lst,n};
            for(j=lst;j<=n;j++) hang[i][j]=tot;
        }
    }
    int tmp = all;
    for(i=1;i<=m;i++) {
        lst=1;
        for(j=1;j<=n;j++) {
            if(mp[j][i]=='#') {
                if(j!=1&&lst<=j-1) {
                    a[++tot]=(node){1,i,lst,j-1};
                    for(k=lst;k<j;k++) lie[k][i]=tot;
                }
                lst=j+1;
            }
        }
        if(lst<=n) {
            a[++tot]=(node){1,i,lst,n};
            for(j=lst;j<=n;j++) lie[j][i]=tot;
        }
    }
    for(i=1;i<=n;i++) {
        for(j=1;j<=m;j++) {
            if(mp[i][j]=='*') {
                int x=hang[i][j],y=lie[i][j];
                add(x,y); add(y,x);
            }
        }
    }
    int ans=0;
    for(i=1;i<=tmp;i++) {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
    }
    printf("%d\n",ans);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325315084&siteId=291194637