存档纪念
存档的原因是WA了很多次 然后试了很多样例终于试出来了
有很多要注意的地方 等以后详细说
参考样例在最后
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
//typedef pair<int, int> P;
/*
二分图最大匹配最大流简化做法,复杂度O(|V||E|)
*/
const int inf = 2147483647;
const double eps = 1e-8;
const int maxn = 2500;
const int N = 55;
const ll mod = 1e9+7;
typedef pair<int,int> pa;
char map[N][N];
pa save[N][N];
int r, c;
int p, q;
vector<int> G[maxn];
int match[maxn]; //匹配
bool used[maxn];
void addedge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
}
//dfs寻找增广路
bool dfs(int v){
used[v] = true;
for(int i=0; i<G[v].size(); i++){
int u = G[v][i], w = match[u];
if(w<0 || !used[w] && dfs(w)){ //u未匹配过,或还有增广路
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
//求解二分图的最大匹配
int bipartite_matching(){
int res = 0;
memset(match, -1, sizeof(match));
for(int v = 0; v< p + q; v++){
if(match[v] < 0){
memset(used, 0, sizeof(used));
if(dfs(v)){
res++;
}
}
}
return res;
}
int main(){
while(~scanf("%d %d", &r, &c)){
for(int i=0; i<maxn; i++){
G[i].clear();
used[i] = 0;
}
memset(save, -1, sizeof(save));
memset(map, 0, sizeof(map));
for(int i = 0; i < r; i++){
scanf("%s",map[i]);
}
p = 0;
int flag = 0;
for(int i = 0; i < c; i++){
flag = 0;
for(int j = 0; j < r; j++){
if(map[j][i] == '*'){
save[j][i].first = p;
flag = 1;
}
else{
save[j][i].first = -1;
if(flag) p++;
flag = 0;
}
}
if(flag) p++;
}
q = 0;
flag = 0;
for(int i = 0; i < r; i++){
flag = 0;
for(int j = 0; j < c; j++){
if(map[i][j] == '*'){
save[i][j].second = q;
flag = 1;
}
else{
save[i][j].second = -1;
if(flag) q++;
flag = 0;
}
}
if(flag) q++;
}
//
// for(int i = 0; i < r; i++){
// for(int j = 0; j < c; j++){
// cout<<save[i][j].first<<","<<save[i][j].second<<" ";
// }
// puts("");
// }
//
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(save[i][j].first!=-1&&save[i][j].second!=-1){
// system("pause");
// cout<<"i: "<<i<<" j: "<<j<<endl;
// cout<<save[i][j].first<<" "<<save[i][j].second<<endl;
addedge(save[i][j].first,p + save[i][j].second );
}
}
}
// int ans = bipartite_matching();
// for(int i=0; i<n; i++){
// int m;
// scanf("%d", &m);
// for(int j=0; j<m; j++){
// int b;
// scanf("%d", &b);
// addedge(i, n+b-1);
// }
// }
printf("%d\n", bipartite_matching());
}
return 0;
}
/*
3 4
.***
.***
.***
5 5
.*.*.
*.*.*
.*.*.
*.*.*
.*.*.
16 16
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
50 50
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
50 50
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*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