HDU [P1281]棋盘游戏
二分图求最大匹配
我们以每一个格子为边,以行和列为两个集合,那么求二分图的最大匹配数就是最多能放车的数目,那么什么是重要点呢?就是删掉后会影响最大匹配数的匹配边。
我们求出最大匹配数后,枚举匹配边,将其删掉后,从x集合的每一个未匹配元素出发,找增广路,如果找不到,就说明这是一个重要点。
注意:在寻找增广路的时候,一定不能改变原有的match数组
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int m,n,k,dt[105][105],g[105][105],match[105];
bool f[105],tag[105][105],bj[105];
bool hungarian(int u,bool flag){
for(int i=1;i<=g[u][0];i++){
int v=g[u][i];
if(!f[v]&&!tag[u][v]){
f[v]=1;
if(!match[v]||hungarian(match[v],flag)){
if(flag) match[v]=u;
return 1;
}
}
}
return 0;
}
int main(){
int T=0;
while(~scanf("%d%d%d",&n,&m,&k)){
T++;
memset(dt,0,sizeof(dt));
memset(g,0,sizeof(g));
memset(match,0,sizeof(match));
memset(bj,0,sizeof(bj));
for(int i=1;i<=k;i++){
int x=init(),y=init();
dt[x][y]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(dt[i][j]) g[i][++g[i][0]]=j;
}
}
int ans=0;
for(int i=1;i<=n;i++){
memset(f,0,sizeof(f));
if(hungarian(i,1)) ans++;
}
int cnt=0;
for(int i=1;i<=m;i++) if(match[i]) bj[match[i]]=1;
for(int i=1;i<=m;i++){
if(match[i]){
tag[match[i]][i]=1;
int t=match[i];bj[t]=0;
match[i]=0;
bool fff=0;
for(int j=1;j<=n;j++){
memset(f,0,sizeof(f));
if(!bj[j]&&hungarian(j,0)) {fff=1;break;}
}
if(fff) cnt++;
match[i]=t;bj[t]=1;
tag[match[i]][i]=0;}
}
printf("Board %d have %d important blanks for %d chessmen.\n",T,ans-cnt,ans);
}
return 0;
}
HDU [P1281]棋盘游戏的更多相关文章
- hdu 1281 棋盘游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu 1281 棋盘游戏 (二分匹配)
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1281 棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1281——棋盘游戏——————【最大匹配、枚举删点、邻接表方式】
棋盘游戏 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
- HDU 1281 - 棋盘游戏 - [二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1281 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 1281 棋盘游戏 【二分图最大匹配】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1281 题意概括: 有N*M大的棋盘,要在里面放尽量多的“车”,求最多能放的车的个数,和为了放最多的车有多 ...
- HDU——1281 棋盘游戏
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1281 棋盘游戏(二分图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 每行每列作为节点,给可以放车的点将对应的行列连边,最多可放置的车即为建二分图后的最大匹配,跑一边 ...
随机推荐
- UVALive3882-And Then There Was One-约瑟夫问题-递推
And Then There Was One Time limit: 3.000 seconds Let's play a stone removing game. Initially, n ston ...
- BZOJ 2823: [AHOI2012]信号塔
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2823 随机增量法.不断加点维护圆,主要是三点共圆那里打得烦(其实也就是个两中垂线求交点+联立方 ...
- webpack的安装与使用
在安装 Webpack 前,你本地环境必须已安装nodejs. 可以使用npm安装,当然由于 npm 安装速度慢,也可以使用淘宝的镜像及其命令 cnpm,安装使用介绍参照:使用淘宝 NPM 镜像. 使 ...
- light oj 1152 Hiding Gold
题目: You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x ...
- tp路由+伪静态+去掉index.php
浏览:10536 发布日期:2013/10/08 分类:技术分享 关键字: 路由 伪静态 去掉index.php 之前一个网友说能不能达到这样的效果,www.olcms.com/news/id.htm ...
- Cannot declare class app\home\controller\Cases because the name is already in use
Cannot declare class app\home\controller\Cases because the name is already in use 命名空间冲突了 use 模型类的时候 ...
- centos利用yum安装卸载软件常用命令
来自:http://tech.v01.cn/Linuxchangjianwenti/changyongruanjiananzhuangyucao/2012/0119/70.html 一.使用yum安装 ...
- java开发常用jar包
mail.jar与activation.jar 里面包含了activation.jar和mail.jar两个包.通过里面的类的调用便可以达到发送电子邮件的目的 commons-beanutils.ja ...
- js双向绑定和地址传递带来的痛苦解决方案之对象拷贝
function cloneObj(obj) { var newObj = {}; if (obj instanceof Array) { newObj = []; } for (var key in ...
- 从(0,0)到(m,n),每次走一步,只能向上或者向右走,有多少种路径走到(m,n)
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...