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. 文件权限和访问控制列表ACL (1)

    背景知识: 文件的权限主要针对三类对象进行定义 Owner: 属主u Group: 属组g Other: 其他o 每个文件针对每一类的访问者都设定了三种权限 r: Readable 读 w: Writ ...

  2. 太鸡冻了!我用 Python 偷偷查到暗恋女生的名字

    1 目 标 场 景 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人 ...

  3. sql server 存储过程的(包含事务)方法里面,采用游标循环,批量删除(修改)数据

    sqlserver 数据库 1.下面是完整的 在存储过程中 使用游标进行 循环删除的实例(包括存储过程中,事务的应用) 2.有问题的话,欢迎随时讨饶我,相信大家看下注释应该就能明白了,很简单的一个,小 ...

  4. springboot配置字符编码

    这边主要有两种方式 方式一.使用传统的Spring提供的字符编码过滤器(Filter的方式) 因为,字符编码过滤器在框架中已经有了,所以我们不需要自己写了.直接进行配置类的实现: @Configura ...

  5. java 接口一

    一 接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口 的子类)来完成.这样将功能的 ...

  6. golang的 strconv 包

    前言 不做文字搬运工,多做思路整理 就是为了能速览标准库,只整理我自己看过的...... 注意!!!!!!!!!! 单词都是连着的,我是为了看着方便.理解方便才分开的 1.strconv 中文文档 [ ...

  7. centos go 安装 使用

    #goland 确保能ping通百度[root@z my_project]# vi /etc/resolv.conf# Generated by NetworkManagersearch locald ...

  8. C++ 不具有继承关系的类之间的显式,隐式转换 2013-07-11 15:41

    好久没有写blog了,今天在学习c#的时候看到某一章节 讲类的隐式与显式转换.特此留笔,以供后续参考之用. 关于显式,隐式转换有些争论,说什么不建议隐式转换.但是个人认为非必要,如果有良好的基础书写基 ...

  9. peer_manager_handler: Connection security failed: role: Peripheral, conn_handle: 0x0, procedure: Bonding, error: 133

    调试nordic 52840 hids_keyboard 例程时,和手机配对,配对失败,提示:peer_manager_handler: Connection security failed: rol ...

  10. JavaScript学习系列博客_17_JavaScript中的函数的参数、返回值

    数的形参(形式参数) - 定义函数时,可以在()中定义一个或多个形参,形参之间使用英文逗号隔开:定义形参就相当于在函数内声明了对应的变量但是并不赋值,形参会在调用时才赋值. 函数的实参(实际参数) - ...