FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))
Description
Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There are totally m pets in the shop, numbered from 1 to m. One day, there are n customers in the shop, which are numbered from 1 to n. In order to sell pets to as
more customers as possible, each customer is just allowed to buy at most one pet. Now, your task is to help the manager to sell as more pets as possible. Every customer would not buy the pets he/she is not interested in it, and every customer would like to
buy one pet that he/she is interested in if possible.
Input
There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the first line of each test case, there are three numbers n, m(0≤n,m≤100) and e(0≤e≤n*m). Here, n and m represent the number of customers and
the number of pets respectively.
In the following e lines of each test case, there are two integers x(1≤x≤n), y(1≤y≤m) indicating that customer x is not interested in pet y, such that x would not buy y.
Output
For each test case, print a line containing the test case number (beginning with 1) and the maximum number of pets that can be sold out.
Sample Input
1
2 2 2
1 2
2 1
Sample Output
Case 1: 2
Source
题意:有n个人分别相应不喜欢某个宠物,问最多能卖出去几仅仅
思路:简单的二分图匹配。匈牙利算法模板
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 110; int g[MAXN][MAXN], vis[MAXN];
int lined[MAXN];
int n, m, e; int dfs(int u) {
for (int v = 1; v <= m; v++) {
if (g[u][v] && !vis[v]) {
vis[v] = 1;
if (lined[v] == -1 || dfs(lined[v])) {
lined[v] = u;
return 1;
}
}
}
return 0;
} int main() {
int t, cas = 1;;
scanf("%d", &t);
while (t--) {
memset(g, 1, sizeof(g));
memset(lined, -1, sizeof(lined));
scanf("%d%d%d", &n, &m, &e);
for (int i = 0; i < e; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a][b] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i))
ans++;
}
printf("Case %d: ", cas++);
printf("%d\n", ans);
}
return 0;
}
FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))的更多相关文章
- fzu 2039 Pets (简单二分图 + (最大流 || 二分图))
Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...
- UVA 2039 Pets(网络流)
Problem Description Are you interested in pets? There is a very famous pets shop in the center of th ...
- 博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏
Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母&quo ...
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- hdu 3829 Cat VS Dog 二分图匹配 最大点独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Prob ...
- HDOJ 5090 Game with Pearls 二分图匹配
简单的二分图匹配: 每个位置可以边到这些数字甚至可以边 Game with Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- UVA 12549 - 二分图匹配
题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2375 Solved: 1005[Submit][Sta ...
随机推荐
- 3. python 字符串的一般使用
3. python 字符串的一般使用 1.基本操作 1)使用+连接 >>> "abc"+"efg" 'abcefg' >&g ...
- Oracle约束的启用和停用
关于Oracle的约束概念和基本操作,我已经在以前的<Constraint基础概念>.<Constraint的简单操作>两篇文章中有过比较详细的介绍了,但是对于如何停用和启 ...
- NFV/SDN驱动OSS转型
- php的初步了解
刚刚学习PHP,在学校中没有接触过这个语言,但是身边的人都说php好,经过这几天的学习,我对它的基础有了一定的认知和了解,php是“PHP Hypertext Preprocessor”的首字母缩写, ...
- Lichee (五) sysconfig1.fex 配置系统
sysconfig配置系统,作为一个通用的软件平台,还希望通过它,可以适应用户不同的方案.通过给出一个对应的配置,用户的方案就可以自动运行,而不需要修改系统里面的代码,或者重新给出参数. 配置脚本的本 ...
- svn报错can only be performed on a version resource [at this time].
报错 can only be performed on a version resource [at this time]. 有的文件能提交,有的文件不能提交 猜想:是不是因为缓存问题方法:tea ...
- 编译和使用 MySQL C++ Connector
记录编译 mysql C and C++ connector 和简单访问数据库. 环境: vs2012, mysql 5.6.13, 基于x64 0. 软件包 mysql http://dev.my ...
- php -- 取路径:getcwd()、__DIR__、__FILE__ 的区别
getcwd() :显示是 在哪个文件里调用此文件 的目录 __DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名
- 2014年ENVI/IDL遥感应用与开发培训班-11月重庆站 開始报名了
主办单位: 中国遥感应用协会 Esri中国信息技术有限公司 内容简单介绍: 依据中国遥感应用协会栾恩杰理事长推动国内遥感技术和应用的指示精神,2014年中国遥感应用协会组织培训交流部与Esri中国信息 ...
- JS中关于in运算符的问题
转自:http://bbs.bccn.net/thread-412608-1-1.html in运算符 in运算符虽然也是一个二元运算符,但是对运算符左右两个操作数的要求比较严格.in运算符要求第1个 ...