USACO 4.2.2 && POJ 1274 —— 二分图匹配

23 篇文章 0 订阅
13 篇文章 0 订阅
The Perfect Stall
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16733 Accepted: 7659

Description

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 

Input

The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

Output

For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

Sample Input

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

Sample Output

4

Source

题意是有n头牛,m个牛栏,每个牛都有自己喜欢的牛栏,问你最多可满足多少牛选择自己喜欢的牛栏

匈牙利算法的模版题。

/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 400 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , m;
int V;
vector<int>G[MAXN];
bool used[MAXN];
int match[MAXN];
void add_edge(int u , int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
}
bool dfs(int v)
{
    used[v] = true;
    for(int i = 0 ;i < G[v].size() ; i++)
    {
        int u = G[v][i] , w = match[u];
        if(w < 0 || !used[w] && dfs(w))
        {
            match[u] = v;
            match[v] = u;
//            cout << u << "--" << v <<endl;
            return true;
        }
    }
    return false;
}
int bipartite()
{
    int res = 0;
    memset(match , -1 , sizeof(match));
    for(int i = 0 ; i < V ; i++)
    {
        if(match[i] < 0)
        {
            memset(used , 0 , sizeof(used));
            if(dfs(i))
            {
//                cout << i << match[i] << endl;
                res++;
            }
        }
    }
    return res;
}
int main()
{
    freopen("stall4.in" , "r" , stdin);
    freopen("stall4.out" , "w" , stdout);
    while(~scanf("%d%d" , &n , &m))
    {
        clr(G , 0);
        for(int i = 0 ;i < n ; i ++)
        {
            int num, x;
            scanf("%d" , &num);
            while(num--)
            {
                scanf("%d" , &x);
//                cout << i << "->" << x - 1 << endl;
                add_edge(i , n + x - 1);
            }
        }
//        for(int u = 0 ; u < 5 ; u++)for(int i = 0 ; i< G[u].size() ; i++)cout << u << "___"<< G[u][i] << endl;
        V = m + n;
        printf("%d\n" , bipartite());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值