Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22736   Accepted: 10144

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


Sample Output

 

Source


  这道题没有什么特别好说的,直接匈牙利算法不解释

Code:

 /**
* poj.org
* Problem#1274
* Accepted
* Time:16ms
* Memory:520k/540k
*/
#include<iostream>
#include<queue>
#include<set>
#include<map>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<stdarg.h>
#include<fstream>
#include<ctime>
using namespace std;
typedef bool boolean;
typedef class Edge {
public:
int end;
int next;
Edge():end(),next(){}
Edge(int end, int next):end(end),next(next){}
}Edge;
int *h;
int _count = ;
Edge* edge;
inline void addEdge(int from,int end){
edge[++_count] = Edge(end,h[from]);
h[from] = _count;
}
int result;
int *match;
boolean *visited;
boolean find(int node){
for(int i = h[node];i != ;i = edge[i].next){
if(visited[edge[i].end]) continue;
visited[edge[i].end] = true;
if(match[edge[i].end] == -||find(match[edge[i].end])){
match[edge[i].end] = node;
return true;
}
}
return false;
}
int n,m;
void solve(){
for(int i = ;i <= n;i++){
if(match[i] != -) continue;
memset(visited, false, sizeof(boolean) * (n + m + ));
if(find(i)) result++;
}
}
int buf;
int b;
boolean init(){
if(~scanf("%d%d",&n,&m)){
result = ;
visited = new boolean[(const int)(n + m + )];
match = new int[(const int)(n + m + )];
edge = new Edge[(const int)((n * m) + )];
h = new int[(const int)(n + m + )];
memset(h, , sizeof(int)*(n + m + ));
memset(match, -,sizeof(int)*(n + m + ));
for(int i = ;i ^ n;i++){
scanf("%d",&buf);
for(int j = ;j ^ buf;j++){
scanf("%d",&b);
addEdge(i + , b + n);
// addEdge(b + n, i + 1);
}
}
return true;
}
return false;
}
void freeMyPoint(){
delete[] visited;
delete[] match;
delete[] edge;
delete[] h;
}
int main(){
while(init()){
solve();
printf("%d\n",result);
freeMyPoint();
}
return ;
}

[题解]poj 1274 The Prefect Stall的更多相关文章

  1. poj 1274 The Prefect Stall - 二分匹配

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22736   Accepted: 10144 Description Far ...

  2. [题解]poj 1274 The Perfect Stall(网络流)

    二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...

  3. Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)

    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...

  4. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

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

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

  6. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  7. POJ 1274 The Perfect Stall (二分图匹配)

    [题目链接] http://poj.org/problem?id=1274 [题目大意] 给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛, 问最多可以满足几头奶牛 [题解] 奶牛和喜欢的草棚连线 ...

  8. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...

  9. [POJ] 1274 The Perfect Stall(二分图最大匹配)

    题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...

随机推荐

  1. SSH 创建证书认证

    *******chmod 600 195-tempuser.txt******* useradd tempusersu tempusercdmkdir .sshchmod 700 .sshcd .ss ...

  2. Android-Cdma手机定位

    对于Android CDMA手机获取当前位置,其实有更便利的办法,就是哄骗CdmaCellLocation.getBaseStationLatitude(),然则getBaseStationLatit ...

  3. javascript中的对象之间继承关系

    相信每个学习过其他语言的同学再去学习JavaScript时就会感觉到诸多的不适应,这真是一个颠覆我们以前的编程思想的一门语言,先不要说它的各种数据类型以及表达式的不同了,最让我们头疼,恐怕就是面向对象 ...

  4. Nim游戏变种——取纽扣游戏

    (2017腾讯实习生校招笔试题)Calvin和David正在玩取纽扣游戏,桌上一共有16个纽扣,两人轮流来取纽扣,每人每次可以选择取1个或3个或6个(不允许不取),谁取完最后的纽扣谁赢.Cavin和D ...

  5. NSMutableAttributedString 富文本删除线的用法

    #import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributed ...

  6. [zz] be similar with和be similar to的区别

    http://wenda.tianya.cn/question/4cb13da080ee34c9 be similar to后边既可以加物主代词又可以加人,即:be similar to sth/sb ...

  7. .net4缓存笔记

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. 初识C++的类

    //Sales_item.h#ifndef SALESITEM_H #define SALESITEM_H #include <iostream> #include<string&g ...

  9. web页面打开本地app(判断是否安装)

    在应用宝中有APP申请链接: //是否可以打开App不可以跳则到下载页 $(".downNow button").on("click",function(){ ...

  10. List集合中的对象根据属性排序

    集合类List存放的数据,默认是按照放入时的顺序存放的,比如依次放入A.B.C,则取得时候,则也是A.B.C的顺序,实际场景中,有时我们需要根据自定义的规则对List中的元素进行排序,该如何实现呢?看 ...