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 ...
随机推荐
- FastAdmin 开发第四天:初试命令行
FastAdmin 最强大的是命令行 先从 test 表开始. 在 FastAdmin 默认有一个 test 表格,用于命令行 crud 测试. 如何开始? 只需要在项目命令行中输入以下命令就会自动生 ...
- spring与hibernate注解及XML方式集成
spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...
- logstash使用分享
1.logstash时间处理函数 当业务场景需要自有的time字段覆盖@timestamp字段的情况下 需要使用 date { match => ["time", " ...
- panabit允许一台代理服务器只能收QQ企业邮箱,和内网ip通讯,限制除了QQ企业邮箱以外的所有内容规则
环境: 可访公网网的内网网段:192.168.0.0/24(员工网段) 192.168.2.0/24(服务器网段)两个内网网段. 不能访问公网的内网网段:192.168.4.0/24 4网段利用fo ...
- 腾讯高性能RPC开发框架Tars实现服务治理(微服务)
Github:https://github.com/Tencent/Tars 1. 介绍 Tars是基于名字服务使用Tars协议的高性能RPC开发框架,同时配套一体化的服务治理平台,帮助个人或者企业快 ...
- [转]jQuery 读取 xml
XML 文件内容: <?xml version="1.0" encoding="UTF-8"?> <stulist> <stude ...
- Java 序列化接口Serializable详解
一个对象序列化的接口,一个类只有实现了Serializable搜索接口,它的对象才是可序列化的.因此如果要序列化某些类的对象,这些类就必须实现Serializable接口.而实际上,Serializa ...
- 2018 Multi-University Training Contest 6-oval-and-rectangle(hdu 6362)-题解
一.题意 求椭圆内接矩形周长的期望. 二.推导过程 已知$c$,容易得出矩形弦长$d=4a\sqrt{1-\frac{c^2}{b^2}}$ 接下来,矩形周长$p=4c+d=4c+4a\sqrt{1- ...
- hive 创建/删除/截断 表(翻译自Hive wiki)
这里罗列常用操作,更多参考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL- ...
- OpenMP 《并行程序设计导论》的补充代码
▶ 使用 OpenMP 和队列数据结构,在各线程之间传递信息 ● 代码,使用 critical 子句和 atomic 指令来进行读写保护 // queue.h #ifndef _QUEUE_H_ #d ...