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 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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std; int n,m,k;
int link[];
bool visit[],map[][];
bool dfs(int a)
{
for(int i=;i<=m;i++)
if(map[a][i]== && !visit[i])
{
visit[i]=;
if(link[i]== || dfs(link[i]))
{
link[i]=a;
return true;
}
}
return false;
}
int main()
{
int a,b,ans;
int T;
cin>>T;
int cnt=;
while(T--)
{
// memset(visit,0,sizeof(visit));
memset(link,,sizeof(link));
memset(map,,sizeof(map));
cin>>n>>m>>k;
ans=;
for(int i=;i<=k;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=;
}
for(int i=;i<=n;i++)
{
memset(visit,,sizeof(visit));
if(dfs(i))
ans++;
}
printf("Case %d: %d\n",cnt++,ans);
}
}
Pets(匈牙利算法)的更多相关文章
- ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)
//匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...
- 匈牙利算法——S.B.S.
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...
- 匈牙利算法与KM算法
匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
- poj1274(匈牙利算法)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22809 Accepted: 101 ...
- 匈牙利 算法&模板
匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...
- 【入门】匈牙利算法+HNOI2006 hero超级英雄
一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...
- [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- Poj(1466),最大独立集,匈牙利算法
题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total S ...
随机推荐
- python1.3集合知识点:
#定义集合:{},集合是只有key没有value的字典,集合内元素不能重复!a={1,2,3,4,5,6}#列表转换成集合b=set([1,2,3,4,5])print(a,b) #集合对列表进行去重 ...
- JVM与Java体系结构
参考笔记:https://blog.csdn.net/weixin_45759791/article/details/107322503 前言 作为Java工程师的你曾被伤害过吗?你是否也遇到过这些问 ...
- 06 大数据CentOS6.5mini安装与网络配置
1. CentOS6.5mini安装 文件>>新建虚拟机 选择自定义,下一步 默认,下一步 选择稍后安装操作系统,下一步 选择CentOS版本,下一步 给虚拟机命名,这个是在VMWare中 ...
- C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...
- vue.extend和vue.component的区别
vue.extend 使用基础 Vue 构造器函数,通过原型继承,(返回)创建一个"子类"(构造器).参数是一个包含组件选项的对象. const Sub = function Vu ...
- 【NOI2014】动物园 - KMP
题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习算法. 某天, ...
- GridBagConstraints详解
名称 作用 默认值 常量 位置 gridx 行(x)的第一个单元格 0并且为非负数 RELATIVE(相对的) 紧跟前一个组件的后面 gridy 列(y)的第一个单元格 0并且为非负数 RELATIV ...
- go 多线程并发 queue demo
原文链接:Writing worker queues, in Go 1.work.go [root@wangjq queue]# cat work.go package main import &qu ...
- 操作系统-I/O(4)I/O控制方式
I/O控制的方式分为: 程序直接控制方式(最简单的I/O方式) • 无条件传送:对简单外设定时(同步)进行数据传送 • ...
- 计算机网络-网络层(1)IPv4和IPv6
IPv4数据报格式: 版本号 这4比特规定了数据报的IP 协议版本.通过查看版本号,路由器能够确定如何解释IP数据报的剩余部分. 首部长度 以4字节为单位,没有选项的首部长度为5*4=20字节 服务类 ...