LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目
Source
http://www.lightoj.com/volume_showproblem.php?problem=1171
Description
Given an m x n chessboard where some of the cells are broken. Now you are about to place chess knights in the chessboard. You have to find the maximum number of knights that can be placed in the chessboard such that no two knights attack each other. You can't place knights in the broken cells.
Those who are not familiar with chess knights, note that a chess knight can attack eight positions in the board as shown in the picture below.
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case starts with a blank line. The next line contains three integers m, n, K (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively. Each of the next K lines will contain two integers x, y (1 ≤ x ≤ m, 1 ≤ y ≤ n) denoting that the cell(x, y) is broken already. No broken cell will be reported more than once.
Output
For each case of input, print the case number and the maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input
2
8 8 0
2 5 4
1 3
1 4
2 3
2 4
Sample Output
Case 1: 32
Case 2: 6
分析
题目大概说一个n*m的国际象棋棋盘上有些格子不能放棋子,问最多能放几个骑士使得它们都不会处于互相攻击的状态。
棋盘黑白染色,形成二分图,然后就是二分图最大点独立集模型了,结果即为所有点数-二分图最大匹配。
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 44444
#define MAXM 44444*22 struct Edge{
int v,cap,flow,next;
}edge[MAXM];
int vs,vt,NE,NV;
int head[MAXN]; void addEdge(int u,int v,int cap){
edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
edge[NE].next=head[u]; head[u]=NE++;
edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
edge[NE].next=head[v]; head[v]=NE++;
} int level[MAXN];
int gap[MAXN];
void bfs(){
memset(level,-1,sizeof(level));
memset(gap,0,sizeof(gap));
level[vt]=0;
gap[level[vt]]++;
queue<int> que;
que.push(vt);
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(level[v]!=-1) continue;
level[v]=level[u]+1;
gap[level[v]]++;
que.push(v);
}
}
} int pre[MAXN];
int cur[MAXN];
int ISAP(){
bfs();
memset(pre,-1,sizeof(pre));
memcpy(cur,head,sizeof(head));
int u=pre[vs]=vs,flow=0,aug=INF;
gap[0]=NV;
while(level[vs]<NV){
bool flag=false;
for(int &i=cur[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
flag=true;
pre[v]=u;
u=v;
//aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
aug=min(aug,edge[i].cap-edge[i].flow);
if(v==vt){
flow+=aug;
for(u=pre[v]; v!=vs; v=u,u=pre[u]){
edge[cur[u]].flow+=aug;
edge[cur[u]^1].flow-=aug;
}
//aug=-1;
aug=INF;
}
break;
}
}
if(flag) continue;
int minlevel=NV;
for(int i=head[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
minlevel=level[v];
cur[u]=i;
}
}
if(--gap[level[u]]==0) break;
level[u]=minlevel+1;
gap[level[u]]++;
u=pre[u];
}
return flow;
} bool map[222][222];
int dx[]={1,1,-1,-1,2,2,-2,-2};
int dy[]={2,-2,2,-2,1,-1,1,-1};
int main(){
int t,n,m,k;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d%d%d",&n,&m,&k);
memset(map,0,sizeof(map));
int a,b,tot=n*m;
while(k--){
scanf("%d%d",&a,&b);
--a; --b;
map[a][b]=1;
}
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
if(map[i][j]) --tot;
}
}
vs=n*m; vt=vs+1; NV=vt+1; NE=0;
memset(head,-1,sizeof(head));
for(int i=0; i<n*m; ++i){
int x=i/m,y=i%m;
if(map[x][y]) continue;
if(x+y&1) addEdge(i,vt,1);
else{
addEdge(vs,i,1);
for(int j=0; j<8; ++j){
int nx=x+dx[j],ny=y+dy[j];
if(nx<0 || nx>=n || ny<0 || ny>=m || map[nx][ny]) continue;
addEdge(i,nx*m+ny,1);
}
}
}
printf("Case %d: %d\n",cse,tot-ISAP());
}
return 0;
}
LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)的更多相关文章
- POJ1466 Girls and Boys(二分图最大点独立集)
最大点独立集就是无向图中最多的两两不相邻的点集. 二分图最大点独立集=顶点数-二分图最大边独立集(二分图最大匹配) 这一题男女分别作YX部,如果x和y有浪漫关系则连边,如此构造二分图,答案显然就是最大 ...
- BZOJ 1143: [CTSC2008]祭祀river(二分图最大点独立集)
http://www.lydsy.com/JudgeOnline/problem.php?id=1143 题意: 思路: 二分图最大点独立集,首先用floyd判断一下可达情况. #include< ...
- BZOJ 4808: 马(二分图最大点独立集)
http://www.lydsy.com/JudgeOnline/problem.php?id=4808 题意: 思路: 这图中的两个马只能选一个,二选一,很像二分图吧,对能互吃的两个棋子连线,在所选 ...
- 【bzoj4808】【马】二分图最大点独立集+简单感性证明
(上不了p站我要死了,侵权度娘背锅) Description 众所周知,马后炮是中国象棋中很厉害的一招必杀技."马走日字".本来,如果在要去的方向有别的棋子挡住(俗称"蹩 ...
- POJ 2771 Guardian of Decency (二分图最大点独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6133 Accepted: 25 ...
- HDU--3829--Cat VS Dog【最大点独立集】
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3829 题意:动物园有n条狗.m头猫.p个小孩,每一个小孩有一个喜欢的动物和讨厌的动物.如今动物园要转移一些 ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- hdu 3829 Cat VS Dog 二分图匹配 最大点独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Prob ...
- 【洛谷】4304:[TJOI2013]攻击装置【最大点独立集】【二分图】2172: [国家集训队]部落战争【二分图/网络流】【最小路径覆盖】
P4304 [TJOI2013]攻击装置 题目描述 给定一个01矩阵,其中你可以在0的位置放置攻击装置. 每一个攻击装置(x,y)都可以按照“日”字攻击其周围的8个位置(x-1,y-2),(x-2,y ...
随机推荐
- 第四章 电商云化,4.2 集团AliDocker化双11总结(作者: 林轩、白慕、潇谦)
4.2 集团AliDocker化双11总结 前言 在基础设施方面,今年双11最大的变化是支撑双11的所有交易核心应用都跑在了Docker容器中.几十万Docker容器撑起了双11交易17.5万笔每秒的 ...
- AE开发使用内存图层
AE开发中,有时需要从磁盘中读取一些文件信息如坐标点转为图层并进行分析,此过程并不需要坐标点入库之类的操作,就可以创建一个内存图层解决问题.创建内存图层需要用到InMemoryWorkspaceFac ...
- ASP.NET中使用JqGrid完整实现
文章提纲 介绍 & 使用场景 JqGrid的一些说明 JqGrid和ASP.NET整合详细步骤 前置准备 框架搭建 数据填充 数据增/删/改 其他 介绍&使用场景 JqGrid不是一个 ...
- ExtJs 实现表单联动
最近做的项目使用Extjs.遇到表单联动的业务.下面来说说主要实现思想: 说明:表单联动一般存在从属关系,有大范围的对象和大范围中的小对象.比如地理位置的选定(例:浙江省-杭州市-某某县).在这里,我 ...
- curl命令使用
curl命令可以用来构造http请求.参数有很多,常用的参数如下: 通用语法:curl [option] [URL...]在处理URL时其支持类型于SHELL的名称扩展功能,如http://www.j ...
- Html5 杂记
(一):html5的声明 <!DOCTYPE html> 注意:声明必须是 HTML 文档的第一行,位于 <html> 标签之前. 声明不是 HTML 标签:它是指示 we ...
- 认识VTK工作原理
VTk通过数据流实现变信息为图形数据的. 数据流一般为:source-filter--mapper--actor--render--renderwindow--interactor. 要理解工作原理, ...
- Github.com的Git和TortoiseGit图文教程
图文介绍Windows系统下使用 Github账户 + msysgit + TortoiseGit 进行文件管理的方法. 安装 安装mysysgit 下载地址:msysgit 安装过程: 0.启动 1 ...
- mount img
直接挂载img文件有时会有 mount:您必须指定文件系统类型 的错误,但加 -t ext2 等类型还是没用. 这是因为img文件包含了mbr引导导致的问题.解决方法如下: $sudo fdis ...
- iOS开发——高级篇——音频、音乐播放(封装类)
一.简介 简单来说,音频可以分为2种音效又称“短音频”,通常在程序中的播放时长为1~2秒在应用程序中起到点缀效果,提升整体用户体验 音乐比如游戏中的“背景音乐”,一般播放时间较长 播放音频可以使用框架 ...