poj2226(最小点覆盖)

思路:前面有个类似的模型,就不多说了;(此等二分图经典模型)

ps:link & vis数组开小了,然后就是各种悲剧的事情;

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 60;
int gg[maxn * maxn + 10][maxn * maxn + 10];
// vector<vector<int> >G;
int row[maxn][maxn],col[maxn][maxn];
int n,m;
int uN,vN;
char mp[maxn][maxn];
inline void Init(){
	uN = vN = 0;
	MEM(gg, 0);
	MEM(row, 0);
	MEM(col, 0);
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= m;j++){
			if (mp[i][j] == '*' && row[i][j] == 0){
				uN++;
				for (int k = j;k <= m && mp[i][k] == '*';k++)
					row[i][k] = uN;
			}
		}
	}
	for (int i = 1;i <= m;i++){
		for (int j = 1;j <= n;j++){
			if (mp[j][i] == '*' && col[j][i] == 0){
				vN++;
				for (int k = j;k <= n && mp[k][i] == '*';k++)
					col[k][i] = vN;
			}
		}
	}
	for (int i = 1;i <= n;i++){
		for (int j = 1;j <= m;j++){
			if (mp[i][j] == '*'){
				gg[row[i][j]][col[i][j]] = 1;
				// G[row[i][j]].push_back(col[i][j]);
			}
		}
	}
	// cout << "uN = " << uN << endl;
	// cout << "vN = " << vN << endl;
}
bool vis[maxn * maxn];
int link[maxn * maxn];
bool SearchP(int u){
	
	for (int i = 1;i <= max(vN,uN);i++){
		if (gg[u][i] && !vis[i]){
			vis[i] = true;
			if (link[i] == -1 || SearchP(link[i])){
				link[i] = u;
				return true;
			}
		}
	}
	
	// for (int i = 0;i < G[u].size();i++){
	// 	int v = G[u][i];
	// 	if (vis[v]) continue;
	// 	vis[v] = true;
	// 	if (link[v] == -1 || SearchP(link[v])){
	// 		link[v] = u;
	// 		return true;
	// 	}
	// }
	return false;
}
inline void Hungary(){
	int ret = 0;
	MEM(link, -1);
	for (int i = 1;i <= uN;i++){
		MEM(vis, false);
		if (SearchP(i)) ret++;
	}
	printf("%d\n",ret);
	return ;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	while(~scanf("%d%d",&n,&m)){
		// cout << "n = " << n << endl;
		// cout << "m = " << m << endl;
		// G.clear();
		// G.resize(n * m + m);
		for (int i = 1;i <= n;i++)
			scanf("%s",mp[i] + 1);
		Init();
		Hungary();
	}
	return 0;
}


你可能感兴趣的:(图论,二分图匹配)