主题链接:

id=1274">http://poj.org/problem?

id=1274

题目大意:

有N头奶牛(编号1~N)和M个牛棚(编号1~M)。

每头牛仅仅可产一次奶。每一个牛棚也仅仅同意一仅仅牛产奶。

如今给出每头奶牛喜欢去的牛棚的编号。问:最多有多少头奶牛能完毕产奶。

思路:

二分图最大匹配问题,建立一个图,用行来表示奶牛,用列来表示牛棚。将奶牛和喜欢去的牛棚编号

连边。

然后用匈牙利算法DFS版或BFS版求二分图的最大匹配数就可以。这里用了匈牙利算法DFS版。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std; const int MAXN = 220; bool Map[MAXN][MAXN];
bool bMask[MAXN]; int NX,NY;
int cx[MAXN],cy[MAXN]; int FindPath(int u)
{
for(int i = 1; i <= NY; ++i)
{
if(Map[u][i] && !bMask[i])
{
bMask[i] = 1;
if(cy[i] == -1 || FindPath(cy[i]))
{
cy[i] = u;
cx[u] = i;
return 1;
}
}
}
return 0;
} int MaxMatch()
{
int res = 0;
for(int i = 1; i <= NX; ++i)
cx[i] = -1;
for(int i = 1; i <= NY; ++i)
cy[i] = -1; for(int i = 1; i <= NX; ++i)
{
if(cx[i] == -1)
{
for(int j = 1; j <= NY; ++j)
bMask[j] = 0;
res += FindPath(i);
}
}
return res;
} int main()
{
int d,id;
while(~scanf("%d%d",&NX,&NY))
{
memset(Map,0,sizeof(Map)); for(int i = 1; i <= NX; ++i)
{
scanf("%d",&id);
for(int j = 1; j <= id; ++j)
{
scanf("%d",&d);
Map[i][d] = 1;
}
} printf("%d\n",MaxMatch());
} return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ1274 The Perfect Stall【二部图最大匹配】的更多相关文章

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

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

  2. POJ1274_The Perfect Stall(二部图最大匹配)

    解决报告 http://blog.csdn.net/juncoder/article/details/38136193 id=1274">题目传送门 题意: n头m个机器,求最大匹配. ...

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

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

  4. poj1274 The Perfect Stall (二分最大匹配)

    Description Farmer John completed his new barn just last week, complete with all the latest milking ...

  5. POJ1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25739   Accepted: 114 ...

  6. poj--1274--The Perfect Stall(最大匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21665   Accepted: 973 ...

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

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

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

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

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

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

随机推荐

  1. WebSocket是一种协议

    WebSocket,并非HTML 5独有,WebSocket是一种协议.只是在handshake的时候,发送的链接信息头和HTTP相似.HTML 5只是实现了WebSocket的客户端.其实,难点在于 ...

  2. __NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking

    __NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking 我的平台 mac os 10.6 Xcode ...

  3. mina的编码和解码以及断包的处理,发送自己定义协议,仿qq聊天,发送xml或json

    近期一段时间以来,mina非常火,和移动开发一样.异常的火爆.前面写了几篇移动开发的文章,都还不错.你们的鼓舞就是我最大的动力.好了,废话少说.我们来看下tcp通讯吧. tcp通讯对于java来说是非 ...

  4. 打造你自己ajax上传图片

    今天,我们需要的图片上传插件,但是,互联网不提供符合他们的需要和易于使用的.所以我写了自己. 方法1,只使用jquery代码,.代码例如以下 <p> <label>上传图片&l ...

  5. 鸟哥之安裝 CentOS7.x

    http://linux.vbird.org/linux_basic/0157installcentos7.php since 2002/01/01 新手建議 開始閱讀之前 網站導覽 Linux 基礎 ...

  6. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. achieve aop through xml

    The main way to achive AOP is deploying a xml file. Now a xml file is presented to be a explanation ...

  8. 开源图计算框架GraphLab介绍

    GraphLab介绍 GraphLab 是由CMU(卡内基梅隆大学)的Select 实验室在2010 年提出的一个基于图像处理模型的开源图计算框架.框架使用C++语言开发实现. 该框架是面向机器学习( ...

  9. iOS 单元測试之XCTest具体解释(一)

    原创blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS-SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...

  10. Android设计模式(二)--策略模式

    1.定义: The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them inter ...