主题链接:

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. 100M 宽带办理

    http://zj.189.cn/zhuanti/kdsbz#%E5%8D%95%E5%AE%BD%E5%B8%A6%E7%89%B9%E6%83%A0

  2. 调整Tomcat的并发线程到5000+

    调整Tomcat的并发线程数到5000+ 1. 调整server.xml的配置 先调整maxThreads的数值,在未调整任何参数之前,默认的并发线程可以达到40. 调整此项后可以达到1800左右. ...

  3. 使用URLConnection提交请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的通信连接.程序可以通过URLConnection实例向该URL发送请求,读取URL ...

  4. Codeforces 474B Worms 二分法(水

    主题链接:http://codeforces.com/contest/474/problem/B #include <iostream> #include <cmath> #i ...

  5. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

  6. rzsz不能大于4G,securefx传5.2G没有问题,

    rzsz不能大于4G,securefx传5.2G没有问题, 查看系统限制: $ulimit -acore file size          (blocks, -c) 0data seg size  ...

  7. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

  8. 深入 CSocket 编程之阻塞和非阻塞模式

    有时,花上几个小时阅读.调试.跟踪优秀的源码程序,能够更快地掌握某些技术关键点和精髓.当然,前提是对这些技术大致上有一个了解. 我通过几个采用 CSocket 类编写并基于 Client/Server ...

  9. OCP读书笔记(8) - 监控和调优RMAN

    监视RMAN作业 1. 创建rman备份: RMAN> run { allocate channel ch1 type disk; allocate channel ch2 type disk; ...

  10. LeetCode——Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...