FZU 2039-Pets(二分图_最大匹配)

来源:互联网 发布:数据对比分析方法 编辑:程序博客网 时间:2024/05/18 01:23

Problem 2039 Pets

Accept: 302    Submit: 795
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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

12 2 21 22 1

 Sample Output

Case 1: 2

 Source

2011年全国大学生程序设计邀请赛(福州)
题意:n个人m个宠物,接下来q行,每行两个数字x,y,代表编号为x的人不喜欢编号为y的宠物,意思就是x肯定不会选y,求最多可以卖出多少宠物。

思路:基本上是个裸地二分图求最大匹配,这个就是建图有点不同,和一般的二分图反过来标记。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);int mp[1010][1010];int vis[1010];int link[1010];int n,m;int dfs(int x){    int i;    for(i=1;i<=m;i++){        if(mp[x][i]||vis[i])            continue;        vis[i]=1;        if(link[i]==-1||dfs(link[i])){            link[i]=x;            return 1;        }    }    return 0;}int main(){    int T,i;    int q;    int x,y;    int icase=1;    scanf("%d",&T);    while(T--){        memset(mp,0,sizeof(mp));        memset(link,-1,sizeof(link));        scanf("%d %d %d",&n,&m,&q);        while(q--){            scanf("%d %d",&x,&y);            mp[x][y]=1;        }        int cnt=0;        for(i=1;i<=n;i++){            memset(vis,0,sizeof(vis));            if(dfs(i))                cnt++;        }        printf("Case %d: %d\n",icase++,cnt);    }    return 0;}


0 0
原创粉丝点击