题意:给定一个图,L代表陆地,W代表水,C表示不确定,问你最多有多少岛。

析:首先给定的L周围必须是是W,只有这样才是最优的,因为如果是L,那么还得有另外的W来包围,不是最优的,那么剩下的就剩下C了,因为要是L多,那么肯定是一个岛屿只有一个L,这样是最优的,并且周围都是W,所以可以把C看成一个点,然后向周围上下左右连边,如果周围存在C,那么就连一条,最后求一个最大独立集就OK了,二分图的最大独立集等于二分图的顶点数 - 二分图的最大匹配。也就是求二分匹配。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edge[maxn*maxn];
int head[maxn*maxn], cnt;
int G[maxn][maxn];
char s[maxn][maxn]; void addEdge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool vis[maxn][maxn];
bool used[maxn*maxn];
int match[maxn*maxn]; void dfs(int r, int c){
for(int i = 0; i < 4; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(vis[x][y] || !is_in(x, y)) continue;
vis[x][y] = 1;
if(s[x][y] == 'L') dfs(x, y);
else s[x][y] = 'W';
}
} bool dfs(int u){
used[u] = true;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to, w = match[v];
if(w < 0 || !used[w] && dfs(w)){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
int ans = 0;
FOR(i, 0, n) FOR(j, 0, m)
if(!vis[i][j] && s[i][j] == 'L'){
vis[i][j] = 1;
dfs(i, j); ++ans;
}
ms(G, -1); ms(head, -1); cnt = 0;
int idx = 0;
FOR(i, 0, n) FOR(j, 0, m)
if(s[i][j] == 'C') G[i][j] = idx++;
FOR(i, 0, n) FOR(j, 0, m)
if(i+j&1&&~G[i][j]){
for(int k = 0; k < 4; ++k){
int x = dr[k] + i;
int y = dc[k] + j;
if(is_in(x, y) && ~G[x][y]){
addEdge(G[i][j], G[x][y]);
addEdge(G[x][y], G[i][j]);
}
}
}
ms(match, -1);
int cnt = 0;
for(int i = 0; i < idx; ++i) if(match[i] < 0){
ms(used, 0); if(dfs(i)) ++cnt;
}
printf("%d\n", ans += idx - cnt);
return 0;
}

  

Gym 101201G Maximum Islands (最大独立集)的更多相关文章

  1. 【最小割】【Dinic】Gym - 101201G - Maximum Islands

    题意:方格内有些位置是水域,有些位置是陆地,有些位置是被云彩遮挡住了:让你自己规定被云彩遮挡住的地方是陆地还是水域,使得陆地个数最多.(均为四连通块) 显然与陆地邻接的云彩填成水比较优.其他云彩格子填 ...

  2. 2017萧山第5场(2016 Pacific Northwest - Division 1)

    B:Buggy Robot [题意] 一个n*m的地图(1≤n, m≤50),有一个入口和一个出口.给定一个命令序列(上,下,左,右),如果碰到障碍或者边际就忽略.问至少加入或删除多少个的命令,使得能 ...

  3. 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

    A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...

  4. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  5. Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)

    题目: The park management finally decided to install some popular boxing machines at various strategic ...

  6. 2019牛客多校第五场 F maximum clique 1 状压dp+最大独立集

    maximum clique 1 题意 给出一个集合s,求每个子集的最大独立集的权值和(权值是独立集的点个数) 分析 n比较小,一股浓浓的暴力枚举每一个子集的感觉,但是暴力枚举模拟肯定会T,那么想一想 ...

  7. 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)

    问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...

  8. 2019牛客多校第五场F maximum clique 1 最大独立集

    题意:给你n个数,现在让你选择一个数目最大的集合,使得集合中任意两个数的二进制表示至少有两位不同,问这个集合最大是多大?并且输出具体方案.保证n个数互不相同. 思路:容易发现,如果两个数不能同时在集合 ...

  9. 2019牛客暑期多校训练营(第五场)F maximum clique 1 二分图求最大独立集

    https://ac.nowcoder.com/acm/contest/885/F #include <bits/stdc++.h> //CLOCKS_PER_SEC #define se ...

随机推荐

  1. Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别

    Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an arr ...

  2. HR-人力资源管理系统(Human Resources Management System,HRMS)

    人力资源管理系统(Human Resources Management System,HRMS),是指组织或社会团体运用系统学理论方法,对企业的人力资源管理方方面面进行分析.规划.实施.调整,提高企业 ...

  3. Java 8 Lambda表达式之方法引用 ::双冒号操作符

    双冒号运算符就是java中的方法引用,方法引用的格式是类名::方法名. 这里只是方法名,方法名的后面没有括号“()”.--------> 这样的式子并不代表一定会调用这个方法.这种式子一般是用作 ...

  4. expect学习笔记及用法

    expect学习笔记及实例详解 expect的基本用法 expect用法

  5. mysql-10临时表、复制表

    1.创建临时表 mysql临时表在我们需要保存一些临时数据时非常有用. 临时表只在当前连接可见,当关闭连接时,mysql会自动删除表并释放所有空间. 如果使用客户端创建临时表,只有在管不客户端程序时才 ...

  6. IDEA试用期结束以后继续试用(全部失效就更新),IDEA 2018 LICENSE SERVER

    IDEA是一款收费的IDE,但是新用户可以免费试用一段时间,试用期结束可以购买,也可以通过填写License server address来继续使用. 打开IDEA以后,通过Help ----- Re ...

  7. 细说Cookie(转)

    原文地址:http://www.cnblogs.com/fish-li/archive/2011/07/03/2096903.html#undefined Cookie虽然是个很简单的东西,但它又是W ...

  8. (2/24) 快速上手一个webpack的demo

    写在前面:该部分的安装都是基于windows系统的,且此处的webpack的版本为:3.6.0. 1.安装webpack 1.1 安装方法: 用win+R打开运行对话框,输入cmd进入命令行模式.然后 ...

  9. 529. Minesweeper

    ▶ 扫雷的扩展判定.已知棋盘上所有点的情况(雷区 'M',已翻开空白区 'B',未翻开空白区 'E',数字区 '1' ~ '8'),现在给定一个点击位置(一定在空白区域),若命中雷区则将被命中的 M ...

  10. sonar 的使用

    1.依赖: <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-ma ...