Gym 101201G Maximum Islands (最大独立集)
题意:给定一个图,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 (最大独立集)的更多相关文章
- 【最小割】【Dinic】Gym - 101201G - Maximum Islands
题意:方格内有些位置是水域,有些位置是陆地,有些位置是被云彩遮挡住了:让你自己规定被云彩遮挡住的地方是陆地还是水域,使得陆地个数最多.(均为四连通块) 显然与陆地邻接的云彩填成水比较优.其他云彩格子填 ...
- 2017萧山第5场(2016 Pacific Northwest - Division 1)
B:Buggy Robot [题意] 一个n*m的地图(1≤n, m≤50),有一个入口和一个出口.给定一个命令序列(上,下,左,右),如果碰到障碍或者边际就忽略.问至少加入或删除多少个的命令,使得能 ...
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution
A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...
- 要back的题目 先立一个flag
要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...
- Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)
题目: The park management finally decided to install some popular boxing machines at various strategic ...
- 2019牛客多校第五场 F maximum clique 1 状压dp+最大独立集
maximum clique 1 题意 给出一个集合s,求每个子集的最大独立集的权值和(权值是独立集的点个数) 分析 n比较小,一股浓浓的暴力枚举每一个子集的感觉,但是暴力枚举模拟肯定会T,那么想一想 ...
- 最小顶点覆盖(Minimum Vertex Cover)与最大独立集(Maximum Independent Set)
问题描述:就是在图中找最小的点集,使得覆盖所有边. 和独立集等价:独立集问题:在图中找最大的点集,使得点集内的所有点互不相连. 引理:顶点覆盖集和独立集互补. 上面这个引理使得这两个问题可以相互规约, ...
- 2019牛客多校第五场F maximum clique 1 最大独立集
题意:给你n个数,现在让你选择一个数目最大的集合,使得集合中任意两个数的二进制表示至少有两位不同,问这个集合最大是多大?并且输出具体方案.保证n个数互不相同. 思路:容易发现,如果两个数不能同时在集合 ...
- 2019牛客暑期多校训练营(第五场)F maximum clique 1 二分图求最大独立集
https://ac.nowcoder.com/acm/contest/885/F #include <bits/stdc++.h> //CLOCKS_PER_SEC #define se ...
随机推荐
- C#.NET通过Socket实现平行主机之间网络通讯(含图片传输的Demo演示)
在程序设计中,涉及数据存储和数据交换的时候,不管是B/S还是C/S模式,都有这样一个概念:数据库服务器.这要求一台性能和配置都比较好的主机作为服务器,以满足数目众多的客户端进行频繁访问.但是对于一些数 ...
- Django ORM介绍 和字段及字段参数
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- [C++ Primer] 第6章: 函数
参数传递 const形参和实参: 顶层const作用于对象本身, 和其他初始化过程一样, 当用实参初始化形参时会忽略掉顶层const, 换句话说, 形参顶层const被忽略掉了, 当形参有顶层cons ...
- GNU Radio: USRP2 and N2x0 Series
Comparative features list 相对性能清单 Hardware Capabilities: 1 transceiver card slot External PPS referen ...
- [DP题]采药
1775:采药 总时间限制:1000ms内存限制:65536kB 描述 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给 ...
- MapBuilder,操作集合工具类
public class MapBuilder { /** * Creates an instance of {@code HashMap} */ public static <K, V> ...
- ASP.NET Web Pages
ylbtech-.Net-ASP.NET Web Pages: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返 ...
- Unreal Enginer4特性介绍
转自:http://blog.csdn.net/cartzhang/article/details/39401991 一.特性说明 特性 Ue4是一款专业开发高质量游戏的平台开发工具.Ue4的渲染加快 ...
- 条件随机场(CRF)-IIS学习算法
改进的迭代尺度法(Improved Iterative Scaling),在很多模型求解中用到,比如最大熵.CRFs等,对模型是对数线性模型的似然都适用.这个算法的思想也很简单,通俗的理解就是通过两个 ...
- lambda架构简介
1.Lambda架构背景介绍 Lambda架构是由Storm的作者Nathan Marz提出的一个实时大数据处理框架.Marz在Twitter工作期间开发了著名的实时大数据处理框架Storm,Lamb ...