poj 2226 Muddy Fields (二分图)
大意:给定n*m网格, 每个格子为泥地或草地, 可以用一些长度任意宽度为1的木板盖住泥地, 要求不能盖到草地, 求最少要多少块木板能盖住所有泥地.
最小点覆盖板子题, 建图跑最大匹配即可.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 110;
int n, m, clk, f[N*N], vis[N*N];
int col[N*N], raw[N*N], f1[N][N], f2[N][N];
vector<int> g[N*N];
char a[N][N];
int has(int i, int j) {return (i-1)*m+j;}
void add(int i, int j) {
g[i].pb(j),g[j].pb(i);
}
int dfs(int x) {
for (vector<int>::iterator it = g[x].begin(); it!=g[x].end(); ++it) {
int y = *it;
if (vis[y]!=clk) {
vis[y] = clk;
if (!f[y]||dfs(f[y])) return f[y]=x;
}
}
return 0;
}
int main() {
while (~scanf("%d%d", &n, &m)) {
REP(i,1,n) scanf("%s", a[i]+1);
*col = *raw = 0;
REP(i,1,2*n*m) f[i]=0,g[i].clear();
REP(i,1,n) {
REP(j,1,m) if (a[i][j]=='*') {
col[++*col] = has(i,j);
while (a[i][j]=='*') f1[i][j++]=col[*col];
--j;
}
}
REP(j,1,m) {
REP(i,1,n) if (a[i][j]=='*') {
raw[++*raw] = has(i,j)+has(n,m);
while (a[i][j]=='*') f2[i++][j]=raw[*raw];
--i;
}
}
REP(i,1,n) REP(j,1,m) if (a[i][j]=='*') add(f1[i][j],f2[i][j]);
int ans = 0;
REP(i,1,*col) {
++clk;
if (dfs(col[i])) ++ans;
}
printf("%d\n", ans);
}
}
poj 2226 Muddy Fields (二分图)的更多相关文章
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- POJ 2226 Muddy Fields 二分图(难点在于建图)
题意:给定一个矩阵和它的N行M列,其中有一些地方有水,现在有一些长度任意,宽为1的木板,要求在板不跨越草,用一些木板盖住这些有水的地方,问至少需要几块板子? 思路:首先想到如果没有不准跨越草的条件则跟 ...
- POJ 2226 Muddy Fields(最小顶点覆盖)
POJ 2226 Muddy Fields 题目链接 题意:给定一个图,要求用纸片去覆盖'*'的位置.纸片能够重叠.可是不能放到'.'的位置,为最少须要几个纸片 思路:二分图匹配求最小点覆盖.和放车那 ...
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields(最小覆盖点+构图)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2226 Muddy Fields (二分匹配)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7340 Accepted: 2715 Desc ...
- POJ 2226 Muddy Fields (二分图匹配)
[题目链接] http://poj.org/problem?id=2226 [题目大意] 给出一张图,上面有泥和草地,有泥的地方需要用1*k的木板覆盖, 有草地的地方不希望被覆盖,问在此条件下需要的最 ...
- poj 2226 Muddy Fields(水二分图)
Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 ...
- POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)
题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...
随机推荐
- Ubuntu 16.04 一键安装P4开发环境记录
写在最前 P4开发环境安装可采用陈翔同学的一键安装脚本:p4Installer p4c-bm是P4-14的编译器,p4c是现在主流P4-16的编译器,bmv2是支持P4运行的软件交换机 系统环境 在安 ...
- Linux安装elasticsearch5全过程(踩坑实录)
Linux版本Centos elasticsearch版本:5.5 1.下载elasticsearch https://artifacts.elastic.co/downloads/elasticse ...
- 使用docker部署mysql主从复制集群
一.环境搭建 虚拟机环境:centos7 IP: 启动3个容器,一个是master,端口是3307,另外两个是slaver,端口是3308和3309 docker pull mysql:5.7 doc ...
- OUC_TeamTraining_#1 720
D - The Mirror of Galadriel Time Limit:2000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- koa 项目实战(七)登录接口
1.登录接口 /** * @route POST api/users/login * @desc 登录接口地址 * @access 接口是公开的 */ router.post('/login', as ...
- centos 如何查看命令是由哪个包提供的
yum whatprovides */ifconfig Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile ...
- Activity的生命周期是谁调用的?
我们知道Activity的生命周期包括onCreate.onStart.onResume.onRestart.onStop.onDestory.onSaveInstanceState.onRestor ...
- itchat库微信自动回复祝福语
过年了,之前看到一些python文章介绍用itchat自动回复微信,我自己就写了一个. 官方文档https://itchat.readthedocs.io/zh/latest/,这个库挺简洁的,对着接 ...
- Python新利器之pipenv
前言 之前学习异步asyncio库的时候,因为asyncio库支持Python3.5以上的版本,而我的Ubuntu14.04只有Python3.4,虽然下载了Python3.6,但是想直接利用ipyt ...
- Java类的加载及初始化
每个类的编译代码都存在于它自己的独立文件中,该文件在需要使用该程序代码时才会被加载.通常有以下三种加载情况: (1) 访问了子类的静态变量或静态方法:仅对类的静态变量,静态块执行初始化操作,并仅初始化 ...