二分图的最大匹配。我是用最大流求解。加个源点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. XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法(转)

    原文:http://blog.csdn.net/wxs0124/article/details/48706685 找到腾讯的info.plist 删除箭头指向的一行 重新打包,上传. (注明,不一定是 ...

  2. SVN—怎样安装SVNserver端软件

    一.怎样安装1.4.5版本号的SVNserver端软件:        a.下载1.4.5版本号的SVNserver端软件.下载地址:http://download.csdn.net/download ...

  3. Java设计模式--------建造者模式(Builder模式)

    Builder模式定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式是一步一步创建一个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容就可以构 ...

  4. 如何删除Oracle数据库

    1>点击开始找Oracle的目录,-->点击[Universal Installer],打开点击[卸载产品] 2>除了oracle_home1 不点外,其他的都勾选. 3>再点 ...

  5. sql server存储过程分页

    Create PROCEDURE [dbo].[Table_GetList] ) = '', -- 查询条件(注意: 不要加 WHERE) ) = '', -- 设置排序 , -- 页尺寸 , -- ...

  6. some knowledge

    注意 关于cornerstone无法上传library文件的问题  上面是我要添加的library文件,网上提供的方法是 在CornerStone的菜单栏里面 View->ShowIgnoreI ...

  7. Matlab基础知识

    一.常用命令:普通的如cd.ls和linux下一样 clc:清除工作窗口中的所有显示内容 clf:清除图形窗口 whos:列出当前工作空间中所有变量,以及它们的名字.尺寸(比如一个矩阵或数组的行列维数 ...

  8. Java提高学习之Object(5)

    字符串形式的表现 Q1:toString() 方法实现了什么功能?A1:toString() 方法将根据调用它的对象返回其对象的字符串形式,通常用于debug. Q2:当 toString() 方法没 ...

  9. php 大数组的POST问题解决

    服务器的PHP.ini的设置问题max_input_vars太小.

  10. C#开发者通用性代码审查清单

    这是为C#开发者准备的通用性代码审查清单,可以当做开发过程中的参考.这是为了确保在编码过程中,大部分通用编码指导原则都能注意到.对于新手和缺乏经验(0到3年工作经验)的开发者,参考这份清单编码会很帮助 ...