二分图的最大匹配。我是用最大流求解。加个源点s和汇点t;s和每只cow、每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to produce milk in )也连一条容量为1的边。然后就用ISAP。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> #define rep(i,l,r) for(int i=l;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x)) using namespace std; const int inf=0x3f3f3f3f,maxn=+; struct edge {
int from,to,cap,flow;
}; struct ISAP {
int n,m,s,t;
vector<edge> edges;
vector<int> g[maxn];
int d[maxn];
int cur[maxn];
int p[maxn];
int num[maxn]; void init(int n) {
this->n=n;
rep(i,,n) g[i].clear();
edges.clear();
clr(d,);
clr(num,);
clr(cur,);
rep(i,,n) num[d[i]]++;
} void addEdge(int from,int to,int cap) {
edges.push_back((edge){from,to,cap,});
edges.push_back((edge){to,from,,,});
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
} int augment() {
int x=t,a=inf;
while(x!=s) {
edge e=edges[p[x]];
a=min(a,e.cap-e.flow);
x=edges[p[x]].from;
}
x=t;
while(x!=s) {
edges[p[x]].flow+=a;
edges[p[x]^].flow-=a;
x=edges[p[x]].from;
}
return a;
} int maxFlow(int s,int t) {
this->s=s; this->t=t;
int flow=;
int x=s;
while(d[s]<n) {
if(x==t) {
flow+=augment();
x=s;
}
int ok=;
rep(i,cur[x],g[x].size()) {
edge e=edges[g[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+) {
ok=;
p[e.to]=g[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok) {
int m=n-;
rep(i,,g[x].size()) {
edge e=edges[g[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==) break;
num[d[x]=m+]++;
cur[x]=;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
} isap; int s() {
int n,m;
cin>>n>>m;
isap.init(n+m+);
rep(i,,n) {
int t;
scanf("%d",&t);
isap.addEdge(,i+,);
rep(j,,t) {
int h;
scanf("%d",&h);
h+=n;
isap.addEdge(i+,h,);
}
}
rep(i,,m) {
int x=i+n+;
isap.addEdge(x,m+n+,) ;
}
return isap.maxFlow(,n+m+);
} int main() {
freopen("stall4.in","r",stdin);
freopen("stall4.out","w",stdout); cout<<s()<<endl; return ;
}

The Perfect Stall
Hal Burch

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.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1: One line with 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.
Line 2..N+1: N lines, each corresponding 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.

SAMPLE INPUT (file stall4.in)

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

OUTPUT FORMAT

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

SAMPLE OUTPUT (file stall4.out)

4

USACO Section 4.2 The Perfect Stall(二分图匹配)的更多相关文章

  1. USACO Section 4.2: The Perfect Stall

    这题关键就在将题转换成最大流模板题.首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流, ...

  2. POJ1274 The Perfect Stall[二分图最大匹配]

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

  3. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

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

  4. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  5. POJ1274 The Perfect Stall 二分图,匈牙利算法

    N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...

  6. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  7. POJ-1274The Perfect Stall,二分匹配裸模板题

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23313   Accepted: 103 ...

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

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

  9. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

随机推荐

  1. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  2. 关于oracle的certview

    前两天去参加oracle  11g  的两门考试(1Z0-051和1Z0-052),在家看了好几遍题库,我本来想着上午一门,下午考一门,但是我嫌着麻烦,就预约一下午考完两门.在考试完一门后,发现成绩不 ...

  3. android listview 三种适配器设置

    1: public class ArrayAdapterActivity extends ListActivity { @Override public void onCreate(Bundle sa ...

  4. html表单提交的几种方法

    原文地址:http://www.ijser.cn/?p=34 最普通最经常使用最一般的方法就是用submit type..看代码: <form name=”form” method=”post” ...

  5. 【双向广搜+逆序数优化】【HDU1043】【八数码】

    HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...

  6. hive 配置MySQL库

    chkconfig mysqld on MySQL开机自启动 建库: --hive数据库2create database hive DEFAULT CHARSET utf8 COLLATE utf8_ ...

  7. 学习使用GitHub(一)--之入门

    因为经常Windows和linux系统交替的使用,在实验室一台电脑,在家一台电脑,自己的电脑和实验室的电脑上面的代码往往没法同步,以前由于种种原因(其实就是懒,没有学习GitHub这样的代码管理工具) ...

  8. 使用chrome调试xpath

    使用chrome调试xpath 相信玩过爬虫的都知道一些库,如lxml(python),可以使用xpath方便地对HTML进行提取,但当真正用的时候,问题就来了,想找到一个元素往往要调试好几遍,而且得 ...

  9. shell基础认识

    Shell 我们在终端下写命令Linux内核是看不懂的必须通过shell解释成内核可执行的代码 这就是shell(其实解释命令这只是它的一个功能模块,shell还可以用来进行程序设计) 有点类似win ...

  10. cursor的形状

    Example:CSS鼠标手型效果 <a href="#" style="cursor:hand">CSS鼠标手型效果</a> Exam ...