POJ1274 The Perfect Stall[二分图最大匹配]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23911 | Accepted: 10640 |
Description
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Input
Output
Sample Input
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
Sample Output
4
Source
裸hungary
http://www.renfei.org/blog/bipartite-matching.html匹配:在图论中,一个「匹配」(matching)是一个边的集合,其中任意两条边都没有公共顶点。最大匹配:一个图所有匹配中,所含匹配边数最多的匹配,称为这个图的最大匹配。
完美匹配:如果一个图的某个匹配中,所有的顶点都是匹配点,那么它就是一个完美匹配。显然,完美匹配一定是最大匹配(完美匹配的任何一个点都已经匹配,添加一条新的匹配边一定会与已有的匹配边冲突)。但并非每个图都存在完美匹配。
交替路:从一个未匹配点出发,依次经过非匹配边、匹配边、非匹配边...形成的路径叫交替路。
增广路:从一个未匹配点出发,走交替路,如果途径另一个未匹配点(出发的点不算),则这条交替路称为增广路(agumenting path)。
最大匹配数:最大匹配的匹配边的数目
最小点覆盖数:选取最少的点,使任意一条边至少有一个端点被选择
最大独立数:选取最多的点,使任意所选两点均不相连
最小路径覆盖数:对于一个 DAG(有向无环图),选取最少条路径,使得每个顶点属于且仅属于一条路径。路径长可以为 0(即单个点)。定理1:最大匹配数 = 最小点覆盖数(这是 Konig 定理)
定理2:最大匹配数 = 最大独立数
定理3:最小路径覆盖数 = 顶点数 - 最大匹配数
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,s,u,v;
struct edge{
int v,ne;
}e[N*N<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int vis[N],le[N];
bool find(int u){
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]){
vis[v]=;
if(!le[v]||find(le[v])){
le[v]=u;
return true;
}
}
}
return false;
}
int ans=;
void hungary(){
memset(le,,sizeof(le));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(find(i)) ans++;
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
cnt=;
memset(h,,sizeof(h));
for(int i=;i<=n;i++){
s=read();
while(s--){v=read();ins(i,v);}
}
ans=;
hungary();
printf("%d\n",ans);
}
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,s,u,v,g[N][N];
int vis[N],le[N];
bool find(int u){
for(int i=;i<=m;i++) if(g[u][i]&&!vis[i]){
vis[i]=;
if(!le[i]||find(le[i])){
le[i]=u;
return true;
}
}
return false;
}
int ans=;
void hungary(){
memset(le,,sizeof(le));
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(find(i)) ans++;
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(g,,sizeof(g));
for(int i=;i<=n;i++){
s=read();
while(s--){v=read();g[i][v]=;}
}
ans=;
hungary();
printf("%d\n",ans);
}
}
POJ1274 The Perfect Stall[二分图最大匹配]的更多相关文章
- POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
- poj1274 The Perfect Stall (二分最大匹配)
Description Farmer John completed his new barn just last week, complete with all the latest milking ...
- POJ1274 The Perfect Stall 二分图,匈牙利算法
N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...
- POJ1274 The Perfect Stall【二部图最大匹配】
主题链接: id=1274">http://poj.org/problem? id=1274 题目大意: 有N头奶牛(编号1~N)和M个牛棚(编号1~M). 每头牛仅仅可产一次奶.每一 ...
- POJ1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25739 Accepted: 114 ...
- POJ1274_The Perfect Stall(二部图最大匹配)
解决报告 http://blog.csdn.net/juncoder/article/details/38136193 id=1274">题目传送门 题意: n头m个机器,求最大匹配. ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- poj--1274--The Perfect Stall(最大匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21665 Accepted: 973 ...
随机推荐
- 渡轮问题Ship
题目描述 Palmia河从东往西流过Palmia国,把整个国家分成南北两半.河的两岸各有N个城市,北岸的每一个城市都与南岸的一个城市互为友好城市,而且任意两个北岸城市的友好城市都不相同.每一对友好城市 ...
- Wijmo 2016 V3发布
互操作性增强 Wijmo继续扩展互操作性包括Angular 2.ReactJS和VueJS. 模块支持 Wijmo最初设计为单个模块. 一切都存储在Wijmo命名空间.Wijmo现在包含很多不同的模块 ...
- Easyui的渲染
锻炼完身体九点到电脑旁,加上整理明天的接口文档,到现在22:38:10:-_-!!,心累 今天整理下Easyui是如何渲染的: <input class="easyui-combobo ...
- git 提交解决冲突
一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. error: You have not concluded your merge (MERGE_HEAD exists) ...
- 自适应备忘录 demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- JavaScript toUpperCase() 方法和 toLowerCase() 方法
1,toUpperCase() 方法用于把字符串转换为大写. 一个新的字符串,在其中 stringObject 的所有小写字符全部被转换为了大写字符. 语法为: stringObject.toUppe ...
- GIS公交查询-flex/java
开发语言是flex.java,开发平台是myeclise.eclise,开发接口是arcgis api for flex,提供以下的功能: 1.站名-站名查询: 2.站点查询: 3.路线查询: 备注: ...
- 从FineReport看开放式引擎API
对于一款软件或产品,尤其是一些企业级应用的IT软件,是不可能满足所有需求的.尤其是针对业务化的产品需求,某些个性化的需求就要进行二次开发.二次开发需要API接口,无论是什么样的开发,开发人员都需要对开 ...
- 关于Fragment你所需知道的一切!
转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...
- 【代码笔记】iOS-图文混排(HBLabelDemo)
一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...