POJ1274 The Perfect Stall_洛谷P1894 [USACO4.2]完美的牛栏

解决农夫约翰新牛棚的奶牛与牛栏最优匹配问题,通过二分图最大匹配算法实现,确保每头奶牛能在喜欢的牛栏中产奶。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目描述

农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶。上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶)。一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶。

给出奶牛们的爱好的信息,计算最大分配方案。

输入输出格式

输入格式:

第一行 两个整数,N (0 <= N <= 200) 和 M (0 <= M <= 200) 。N 是农夫约翰的奶牛数量,M 是新牛棚的牛栏数量。

第二行到第N+1行 一共 N 行,每行对应一只奶牛。第一个数字 (Si) 是这头奶牛愿意在其中产奶的牛栏的数目 (0 <= Si <= M)。后面的 Si 个数表示这些牛栏的编号。牛栏的编号限定在区间 (1..M) 中,在同一行,一个牛栏不会被列出两次。

输出格式:

只有一行。输出一个整数,表示最多能分配到的牛栏的数量.

输入输出样例

输入样例#1:
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
输出样例#1:
4

说明

N (0 <= N <= 200)

M (0 <= M <= 200)

二分图最大匹配,匈牙利很稳,然而在洛谷上 P1894 能过,

POJ 就 WA 了,玄学问题。。。

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define MAXN 210
using namespace std;
int n,m,s=0,ans=0;
int f[MAXN],a[MAXN][MAXN];
bool vis[MAXN];
bool find(int x){
     for(int i=1;i<=m;i++){
             if(!vis[i]&&a[x][i]){
                                  vis[i]=true;
                                  if(f[i]==-1||find(f[i])){
                                                           f[i]=x;
                                                           return true;
                                                           }
                                  }
             }
     return false;
}
int main(){
    int x,y;
    memset(a,0,sizeof(a));
    memset(f,-1,sizeof(f));
    memset(vis,false,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
            scanf("%d",&x);
            for(int j=1;j<=x;j++){
                    scanf("%d",&y);
                    a[i][y]=1;
                    }
            }
    for(int i=1;i<=n;i++){
            memset(vis,false,sizeof(vis));
            if(find(i))
            s++;
            ans=max(s,ans);
            }
    printf("%d\n",ans);
    return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值