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

2011年全国大学生程序设计邀请赛(福州)

题意:有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;
}
时间: 2024-08-28 17:21:31

FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))的相关文章

第一次组队赛---2010年全国大学生程序设计邀请赛(福州)L

L - Left Mouse Button Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 1920 Description Mine sweeper is a very popular small game in Windows operating system. The object of the game is to find mi

fzu 2039 Pets (简单二分图 + (最大流 || 二分图))

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

博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏

Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母"O"或点号"."之一,分别表示对应的棋盘格中有黑色棋子.有白色棋子和没有棋子.其中点号"."恰好出现一次. 接下来一行包含一个整数 k(1≤k≤1000) ,表示兔兔和蛋蛋各进行了k次操作. 接下来 2k行描述一局游戏的过程.其中第 2i – 1行是兔兔的

hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Problem Description The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the

HDOJ 5093 Battle ships 二分图匹配

二分图匹配: 分别按行和列把图展开,hungary二分图匹配.... 样例: 4 4 *ooo o### **#* ooo* 按行展开.... *ooo o#oo oo#o ooo# **#o ooo* ooo* 再按列展开... 7 * 8 *ooooooo oooooooo oooooooo oooooooo *o*ooooo ooooooo* ooooooo* 匹配结果3 Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memo

HDOJ 5090 Game with Pearls 二分图匹配

简单的二分图匹配: 每一个位置的数可能边成那些数连边即可 Game with Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 122    Accepted Submission(s): 85 Problem Description Tom and Jerry are playing a game with tubes a

POJ2584 T-Shirt Gumbo 二分图匹配(网络流)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 const int inf=0x3f3f3f3f; 6 const int sink=30; 7 8 struct Edge 9 { 10 int to; 11 int next; 12 int capacity; 13 14 void assign(int t,int n,int c) 15 { 16 to=t; next=n; ca

棋盘游戏(二分图匹配)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3200    Accepted Submission(s): 1897 Problem Description 小 希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放

BZOJ 1854 游戏(二分图匹配或并查集)

此题的二分图匹配做法很容易想,就是把属性当做s集,武器当做t集,如果该武器拥有该武器则连一条边. 那么答案就是求该二分图的最大前i个匹配.将匈牙利算法改一改,当前找不到增广路就break. 但是过这个题需要常数优化,不能每次都fillchar一遍used数组.可以用队列将使用的used点加入,然后需要初始化的时候弹出即可. # include <cstdio> # include <cstring> # include <cstdlib> # include <i