FZU 2039 Pets 二分圖匹配

Pets

Accept: 278    Submit: 751
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年全國大學生程序設計邀請賽(福州)



#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8

int n,m,e;
int mp[111][111];
int linkk[111],use[111];

bool dfs(int x)
{
    int i;
    for(i=1;i<=m;i++)
    {
        if (use[i]==0&&mp[x][i])
        {
            use[i]=1;
            if (linkk[i]==-1||dfs(linkk[i]))
            {
                linkk[i]=x;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    CLR(linkk,-1);
    int num=0;
    int i;
    for(i=1;i<=n;i++)
    {
       CLR(use,0);
       if(dfs(i))
          num++;
    }
    return num;
}



int main()
{
    int _;
    cin>>_;
    REP(cas,_){
        cin>>n>>m>>e;
        CLR(mp,1);
        int x,y;
        REP(i,e){
            scanf("%d%d",&x,&y);
            mp[x][y]=0;
        }
        printf("Case %d: %d\n",cas+1,hungary());
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章