(step6.3.5)hdu 1281(棋盘游戏——二分图的完美匹配)
题目大意:本体是中文题。读者可以直接在OJ上看
解题思路:
1)完美匹配:所有的端点都是匹配点
2)对于二分图的完美匹配,我们需要用一个数组来存储匹配点。(而二分图的其他问题(我们则可以直接使用变量来存储即可)
for(i = 1 ; i <= k ; ++i){
// int a,b对于完美匹配的题,需要用数组记录下匹配点。假如不是完美匹配的二分图的题。直接用a,b即可
scanf("%d%d",&a[i],&b[i]);
map[a[i]][b[i]] = 1;
}
3)解决完美匹配问题的核心代码:
代码如下:
int count = 0;
for(i = 1 ; i <= k ; ++i){
map[a[i]][b[i]] = 0;
int d = max_match();
map[a[i]][b[i]] = 1;
if(d != num){//如果d!=num,则说明该点是匹配点.
count++;
}
}
代码如下:
/*
* 1281_1.cpp
*
* Created on: 2013年8月31日
* Author: Administrator
*/ #include <iostream> using namespace std; const int maxn = 101;
int map[maxn][maxn];
int link[maxn];
bool useif[maxn];
int n,m; int can(int t){
int i;
for(i = 1 ; i <= m ; ++i){
if(useif[i] == 0 && map[t][i]){
useif[i] = 1;
if(link[i] == -1 || can(link[i]) ){
link[i] = t;
return 1;
}
}
}
return 0;
} int max_match(){
int i;
int num = 0;
memset(link,-1,sizeof(link));
for(i = 1 ; i <= n ; ++i){
memset(useif,0,sizeof(useif));
if(can(i)){
num++;
}
}
return num;
} int main(){
int k;
int counter = 1;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
int i;
int a[k+1],b[k+1];
memset(map,0,sizeof(map));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i = 1 ; i <= k ; ++i){
// int a,b对于完美匹配的题,需要用数组记录下匹配点。假如不是完美匹配的二分图的题。直接用a,b即可
scanf("%d%d",&a[i],&b[i]);
map[a[i]][b[i]] = 1;
} int num = max_match(); int count = 0;
for(i = 1 ; i <= k ; ++i){
map[a[i]][b[i]] = 0;
int d = max_match();
map[a[i]][b[i]] = 1;
if(d != num){//如果d!=num,则说明该点是匹配点.
count++;
}
} printf("Board %d have %d important blanks for %d chessmen.\n",counter++,count,num);
}
}
(step6.3.5)hdu 1281(棋盘游戏——二分图的完美匹配)的更多相关文章
- HDU 1281 - 棋盘游戏 - [二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1281 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- codevs 1222 信与信封问题(二分图的完美匹配)
1222 信与信封问题 题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封. ...
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVa 11383 少林决胜(二分图最佳完美匹配)
https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...
- Ants(二分图最佳完美匹配)
Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6904 Accepted: 2164 Special Ju ...
- UVA - 1045 The Great Wall Game(二分图最佳完美匹配)
题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...
- hdu 1281 棋盘游戏
http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 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 ...
随机推荐
- 编译ycm库
在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...
- linux线程之pthread_join和pthread_detach
在任何一个时间点上,线程是可结合的(joinable)或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死.在 被其他线程回收之前,它的存储器资源(例如栈)是不释放的.相反 ...
- winform treeview 通过节点名称添加子节点
/// <summary> /// 添加人员节点(利用TreeNodeCollection) /// </summary> /// <param name="t ...
- 用JS判断两个数字的大小
js中的var定义的变量默认是字符串,如果单纯的比较字符串的话,会出现错误,需要先转化为int类型在做比较. [备注:110和18在你写的程序中是18大的,因为 这两个数都是字符串,而1和1相等之后比 ...
- zoj 1409 Communication System
/*如果要一个物体的多种属性,最好用结构体,不要用二维数组或者多维数组.用多维数组进行关键字排序很不稳定 */ /*给每个设备的所有价格排序,每个设备选取恰好比已知带宽大的价格(这个时候的比例最大) ...
- 搜索(BFS)
Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and t ...
- activemq demo指南
queue与topic的技术特点对比 topic queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状态 topic ...
- iptables 简单配置
通过命令 netstat -tnl 可以查看当前服务器打开了哪些端口 Ssh代码 netstat -tnl 查看防火墙设置 Ssh代码 iptables -L -n 开放 ...
- html 表头固定
<div style="width: 100%; min-width: 300px; padding-right: 10px;"> <table style=&q ...
- Eclipse运行慢
http://blog.csdn.net/chrissata/article/details/7759836 http://blog.csdn.net/heyutao007/article/detai ...