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(匈牙利算法)的更多相关文章

  1. ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

    //匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...

  2. 匈牙利算法——S.B.S.

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

  3. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  4. HDU1054 Strategic Game——匈牙利算法

    Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...

  5. poj1274(匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22809   Accepted: 101 ...

  6. 匈牙利 算法&模板

    匈牙利 算法 一. 算法简介 匈牙利算法是由匈牙利数学家Edmonds于1965年提出.该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法. 二分图的定义: 设G=(V,E)是一个 ...

  7. 【入门】匈牙利算法+HNOI2006 hero超级英雄

    一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...

  8. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  9. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  10. Poj(1466),最大独立集,匈牙利算法

    题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total S ...

随机推荐

  1. 改改Python代码,运行速度还能提升6万倍

    这份最新研究指出,在后摩尔定律时代,人类所获得的的算力提升将更大程度上来源于计算堆栈的「顶层」,即软件.算法和硬件架构,这将成为一个新的历史趋势. 很多人学习python,不知道从何学起.很多人学习p ...

  2. 学习Hibernate5 JPA这一篇就够了

    配套资料,免费下载 链接:https://pan.baidu.com/s/158JkczSacADez-fEMDXSYQ 提取码:2ce1 复制这段内容后打开百度网盘手机App,操作更方便哦 第一章 ...

  3. dom4j基本操作

    DOM4J与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml. ...

  4. 【计算机算法设计与分析】——NP

    时间复杂度 时间复杂度并不是表示一个程序解决问题需要花多少时间,而是当问题规模扩大后,程序需要的时间长度增长得有多快.也就是说,对于高速处理数据的计算机来说,处理某一个特定数据的效率不能衡量一个程序的 ...

  5. JavaFX桌面应用开发系列文章

    ~ JavaFX桌面应用开发系列文章汇总篇 ~ JavaFX桌面应用开发-HelloWorld JavaFX布局神器-SceneBuilder JavaFX让UI更美观-CSS样式 JavaFX桌面应 ...

  6. Java中编写代码出现异常,如何抛出异常,如何捕获异常

    异常的产生过程解析 先运行下面的程序,程序会产生一个数组索引越界异常ArrayIndexOfBoundsException.我们通过图解来解析下异常产生的过程. 工具类 class ArrayTool ...

  7. 2020-07-24:聊一下zookeeper的同步算法。

    福哥答案2020-07-24: 同步算法基于 ZAB 协议,一种快速 Paxos 算法. 快速Paxos算法Paxos算法可能出现死循环,就是在两个Proposer总是在交替prepare.并且,Pa ...

  8. JavaScript 回调函数的简单示例

    回调函数 回调函数也被称为高阶函数 所谓高阶函数,就是说值 函数作为参数被传递或者返回值输出 操作函数的函数称为 高阶函数 把一段可执行的代码(一个函数)作为参数传递给其他的代码(另一个函数),并在需 ...

  9. LeetCode746 Min Cost Climbing Stairs(爬上楼梯的最小损失)

    题目 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you p ...

  10. 火题小战 B. barbeque

    火题小战 B. barbeque 题目描述 \(Robbery\) 是一个大吃货(雾) 某个神奇的串由牛肉和青椒构成,于是\(Robbery\)购买了\(n\)个餐包来自己做这个串,每个餐包中有一些牛 ...