The Perfect Stall(二分图最大匹配)

转载 2015年07月09日 20:41:20
The Perfect Stall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20251   Accepted: 9130

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


#include<cstdio>
#include<iostream>
#include<cstring>
const int maxs=215;
using namespace std;
int n,m;
int vis[maxs];
int map[maxs][maxs];
int liked[maxs];
int success(int x)
{
    int i;
    for(i=1;i<=m;i++)
    {
        if(!vis[i]&&map[x][i]==1)
        {
            vis[i]=1;
            if(!liked[i]||success(liked[i]))
            {
                liked[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int k,a,num,i;
    while(cin>>n>>m)
    {
        num=0;
        memset(map,0,sizeof(map));
        memset(liked,0,sizeof(liked));
        for(i=1;i<=n;i++)
        {
            cin>>k;
            while(k--)
            {
                cin>>a;
                map[i][a]=1;
            }
        }
        for(i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(success(i))
            {
                num++;
            }
        }
        cout<<num<<endl;
    }
}


相关文章推荐

usaco 4.2 The Perfect Stall(二分图最大匹配入门题)

The Perfect Stall Hal Burch Farmer John completed his new barn just last week, complete with all t...
  • fp_hzq
  • fp_hzq
  • 2012年07月24日 20:11
  • 512

USACO Section 4.2 The Perfect Stall - 网络流求最大二分图匹配..

用网络流求二分图匹配的方法在算法导论上就看到过了..只是一直没去实现..确实用网络流来解二分图匹配有点大材小用了..一般的求最大匹配用匈牙利算法轻轻松松一分钟啊..但是网络流的解法虽然写起来多..但思...
  • kk303
  • kk303
  • 2012年01月13日 21:58
  • 1270

USACO 4.2.2The Perfect Stall 完美的牛栏(二分图最大匹配)

题目描述: 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都...
  • cqbzwja
  • cqbzwja
  • 2015年07月27日 11:54
  • 1259

POJ-1274-The Perfect Stall【二分图最大匹配】【模板题】

1274-The Perfect Stall

POJ 1274 The Perfect Stall (二分图最大匹配入门题,匈牙利算法)

题目大意: 有n头奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓产奶,每个谷仓最多只能容纳一头奶牛,问最多有多少奶牛能够同时产奶。         输入第一行给出n与...

POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepte...

USACO 4.2 The Perfect Stall网络流解最大二分图匹配

如下图所示方式构图,每条边的容量为1,则从source到sink最大流量就等于cow与stall的最大匹配 代码: #include #include #define MAX 410 us...

POJ_P1274 The Perfect Stall(二分图匹配+匈牙利算法)

Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21468 Accepted: 9664 Descri...

POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求。 2.给你学生数和课程数,以及学生上的...

POJ 1274 The Perfect Stall 网络流 二分图匹配

题目描述:Description Farmer John completed his new barn just last week, complete with all the latest mi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The Perfect Stall(二分图最大匹配)
举报原因:
原因补充:

(最多只允许输入30个字)