hdu 5556 Land of Farms 最大团+暴力
Land of Farms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 539 Accepted Submission(s): 177
Farmer John wants to build as many farms as possible on the new land. It is required that any two farms should not be adjacent. Otherwise, sheep from different farms would fight on the border. This should be an easy task until several ancient farms are discovered.
Each of the ancient farms also consists of one or more connected grids. Due to the respect to the ancient farmers, Farmer John do not want to divide any ancient farm. If a grid from an ancient farm is selected in a new farm, other grids from the ancient farm should also be selected in the new farm. Note that the ancient farms may be adjacent, because ancient sheep do not fight each other.
The problem is a little complicated now. Can you help Farmer John to find a plan with the maximum number of farms?
Each test case starts with a line containing two integers N and M, indicating the size of the land. Each of the following N lines contains M characters, describing the map of the land (1≤N,M≤10). A grid of an ancient farm is indicated by a single digit (0-9). Grids with the same digit belong to the same ancient farm. Other grids are denoted with a single character “.”. It is guaranteed that all test cases are valid.
3 4
..3.
023.
.211
2 3
...
...
4 4
1111
1..1
1991
1111
Case #2: 3
Case #3: 1
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf char f[15][15];
int anc[16],flag[14],mp[12][12],cnta,ans,res;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0},match[105],used[105];
vector<int> G[205];
int cas,r,l;
bitset<15> sta; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
//if(sta==0) cout<<"u: "<<u<<" v:"<<v<<"\n";
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i],w=match[v];
if(w<0||(!used[w]&&dfs(w))){
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bi_mactch()
{
int res=0;
MM(match,-1);
for(int i=1;i<=r;i++) for(int j=1;j<=l;j++){
int k=(i-1)*l+j;
if(match[k]<0){
MM(used,0);
if(dfs(k)) res++;
}
}
return res;
} int par[15]; int findr(int u)
{
if(par[u]!=u)
par[u]=findr(par[u]);
return par[u];
} void unite(int u,int v)
{
int ru=findr(u),rv=findr(v);
if(ru!=rv) par[ru]=rv;
} int num=0;
void sear(int i,int j)
{
if(f[i][j]>='0'&&f[i][j]<='9'){
int k=f[i][j]-'0';
if(!flag[k]) return;
mp[i][j]=-1;
for(int d=0;d<4;d++){
int tx=i+dx[d],ty=j+dy[d];
if(f[tx][ty]>='0'&&f[tx][ty]<='9'){
int k2=f[tx][ty]-'0';
if(flag[k2]) unite(k,k2);
}
else if(f[tx][ty]=='.') mp[tx][ty]=-1;
}
}
} void bgraph()
{
for(int i=1;i<=110;i++) G[i].clear();
for(int i=1;i<=r;i++)
for(int j=1;j<=l;j++)
if(f[i][j]=='.'&&!mp[i][j]){
num++;
int k=(i-1)*l+j;
//if(sta==0) cout<<"kk:"<<k<<"\n";
if(f[i-1][j]=='.'&&!mp[i-1][j]) add_edge(k,k-l);
if(f[i][j-1]=='.'&&!mp[i][j-1]) add_edge(k,k-1);
}
} void solve()
{
ans=0;
for(int k=0;k<=(1<<cnta)-1;k++){
res=0;MM(mp,0);MM(flag,0);
sta=k;
for(int i=0;i<cnta;i++) if(sta[i]) {
int w=anc[i+1];flag[w]=1;
par[w]=w;
}
for(int i=1;i<=r;i++) for(int j=1;j<=l;j++) sear(i,j);
for(int i=1;i<=cnta;i++)
if(flag[anc[i]]&&par[anc[i]]==anc[i]) res++;
num=0;bgraph();
ans=max(ans,res+num-bi_mactch());
}
} int main()
{
SC("%d",&cas);
int kk=0;
while(cas--){
SC("%d%d",&r,&l);
MM(flag,0);
cnta=0;
for(int i=1;i<=r;i++) {
SC("%s",f[i]+1);
for(int j=1;j<=l;j++)
if(f[i][j]!='.'){
int k=f[i][j]-'0';
if(!flag[k]){
flag[k]=1;
anc[++cnta]=k;
}
}
}
solve();
printf("Case #%d: %d\n",++kk,ans);
}
return 0;
}
分析:
1.先2^10=10^3暴力枚举选择的古代的田,选择了后,在用并查集维护一下,能产生的牧田数。
2.删除选择的古代的田周围的普通田地,再用下网格的二分图,考虑不想邻的连接一条边,那么最后显然成成了,求这样一个最大团
3.二分图的最大团=补图的最大点独立集合=顶点数-最大匹配数
hdu 5556 Land of Farms 最大团+暴力的更多相关文章
- Land of Farms HDU - 5556 二分图匹配
Farmer John and his brothers have found a new land. They are so excited and decide to build new farm ...
- 【HDOJ5556】Land of Farms(最大团)
题意:给定n*m的网格图,上面只有字符'.' 和 数字0-9.其中数字表示这是该格是古老的土地,字符'.'表示该格只是普通的土地. 可以认为一块古老的农田由四联通的所有数字相同的格组成的块,一块普通的 ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- hdu 3183 A Magic Lamp rmq或者暴力
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- HDU 6395 Sequence 【矩阵快速幂 && 暴力】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) ...
- 「国庆训练&知识学习」图的最大独立集与拓展(Land of Farms,HDU-5556)
题意 一个\(N*M\)的矩阵,其中"."代表空地,"0-9"代表古代建筑,我们如果选择了一个编号的古代建筑想要建立,那么对应就要将全部该编号的建筑建立起来,如 ...
- HDU 4462:Scaring the Birds(暴力枚举+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=4462 题意:有一个n*n的地图,有k个空地可以放稻草人,给出每个空地可以放的稻草人属性,属性中有个R代表这个位置 ...
- HDU 6697 Closest Pair of Segments (计算几何 暴力)
2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...
- HDU 3267 Graph Game(博弈论+图论+暴力)
题面传送门 题意: 有一棵 \(n\) 个节点的图 \(G\),R 和 B 两个人轮流操作,R 先操作. 每次操作 R 可以染红任意一条未染色的边,B 可以染蓝任意一条未染色的边 R 的目标是染成一棵 ...
随机推荐
- Kali Linux安装AWVS漏扫工具
Acunetix是全球排名前三的漏洞发现厂商,其全称(Acunetix Web Vulnerability Scanner)AWVS是业内领先的网络漏洞扫描器,其被广泛赞誉为包括最先进的SQL注入和X ...
- DIP常用资源整理
Deep Learning(深度学习): ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习):一 ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习) ...
- MUI 跨域请求web api
由于刚接触MUI框架,所以在跨域问题上花了一点时间.希望我的方式能帮你少走点弯路(大神就直接过里吧)! 首先,遇到这个问题,各种百度.其中说法最多的是将mui,js文件里的 setHeader('X- ...
- TVM图优化(以Op Fusion为例)
首先给出一个TVM 相关的介绍,这个是Tianqi Chen演讲在OSDI18上用的PPThttps://files.cnblogs.com/files/jourluohua/Tianqi-Chen- ...
- Nginx安装与配置【转】
原文:linux之nginx 作者;海燕. 一.nginx Ngix是web服务器,跟apache一样,它可以做动态请求转发.web端负载均衡.反向代理等等: tomcat是应用服务器,当然如果非用逼 ...
- 一个用JavaScript生成思维导图(mindmap)的github repo
github 地址:https://github.com/dundalek/markmap 作者的readme写得很简单. 今天有同事问作者提供的例子到底怎么跑.这里我就写一个更详细的步骤出来. 首先 ...
- SpringCloud之Hystrix容错保护原理及配置
1 什么是灾难性雪崩效应? 如下图的过程所示,灾难性雪崩形成原因就大致如此: 造成灾难性雪崩效应的原因,可以简单归结为下述三种: 服务提供者不可用.如:硬件故障.程序BUG.缓存击穿.并发请求量过大等 ...
- Spring Cloud(七)服务网关 Zuul Filter 使用
上一篇文章中,讲了Zuul 转发,动态路由,负载均衡,等等一些Zuul 的特性,这个一篇文章,讲Zuul Filter 使用,关于网关的作用,这里就不再次赘述了,重点是zuul的Filter ,我们可 ...
- CHD-5.3.6集群上oozie安装
参考文档:http://archive.cloudera.com/cdh5/cdh/5/oozie-4.0.0-cdh5.3.6/DG_QuickStart.html tar -zxvf oozie ...
- (备忘)Linux mount(挂载命令)详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...