主题链接:

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. Oracle dump 分析secondary key

    验证secondary key 含有主键列数据 SQL> select object_name,object_id,OBJECT_TYPE from user_objects; OBJECT_N ...

  2. cape town

    开普敦_百度百科 开普敦

  3. 【MFC两种视频图像採集方法】DirectShow与Opencv

    效果图: DirectShow採集核心代码: 创建线程调用该函数,採集图像通过x264解码封装rtmp协议包.推送至FMSserver,可实现视频直播 UINT __stdcall StartVide ...

  4. 3.1.2 MVC模式和URL访问

    本节课大纲: 一.什么是MVC //了解 M -Model 编写model类 对数据进行操作 使用Model类 来操作数据 V -View 编写html文件,页面呈现 C -Controller 编写 ...

  5. 从iReport至Jaspersoft Studio

    这篇文章同步到http://www.waylau.com/from-ireport-to-jaspersoft-studio/ 从5.5版本号開始,Jaspersoft Studio将代替iRepor ...

  6. java中clone的深入理解

    Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Clone机制是如何工作的?      1. Clone和Copy      假 ...

  7. c#常见stream操作

    原文: c#常见stream操作 常见并常用的stream一共有 文件流(FileStream), 内存流(MemoryStream), 压缩流(GZipStream), 加密流(CrypToStre ...

  8. 改动file header (測)

    --改动file header ------------------------------------------------------------------------- cd $ORACLE ...

  9. window应用移植到Linux下(应用移植)

     配置QT的环境变量,这台电脑à属性à高级系统设置à高级à环境变量à系统变量àpathàC:\Qt\Qt5.3.0\5.3\mingw482_32\bin;C:\Qt\Qt5.3.0\Tools\ ...

  10. CC Subarray LCM (数学)

    题目连接:http://www.codechef.com/problems/SUBLCM 题意:给定一个序列,求最长连续子序列满足 LCM(Ai,Ai+1...Aj) =Ai*Ai+1*...*Aj. ...