题意:

就是尽可能的选多的课

思路:

把课程和上课的时间看作二分图

跑一跑二分匹配就好了

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 350 int cos[10][20];
int ma[N][N];
bool vis[N];
int n;
int cx[N];
int cy[N];
int cnt; //建立课程时间的图
void init()
{
cnt=1;
for(int i=1;i<=7;i++)
{
for(int j=1;j<=12;j++)
{
cos[i][j]=cnt++;
}
}
} int findpath(int u)
{
for(int i=1;i<cnt;i++)
{
if(!vis[i]&&ma[u][i])
{
vis[i]=1;
if(cy[i]==-1||findpath(cy[i]))
{
cy[i]=u;
cx[u]=i;
return 1;
}
}
}
return 0;
} int main()
{
init();
while(~scanf("%d",&n))
{
memset(ma,0,sizeof(ma)); for(int i=1;i<=n;i++)
{
int x,a,b;
scanf("%d",&x);
for(int j=1;j<=x;j++)
{
scanf("%d%d",&a,&b);
ma[i][cos[a][b]]=1;
}
}
memset(cx,-1,sizeof(cx));
memset(cy,-1,sizeof(cy)); int ans=0;
for(int i=1;i<=n;i++)
{
if(cx[i]==-1)
{
memset(vis,0,sizeof(vis));
ans+=findpath(i);
}
}
printf("%d\n",ans);
}
return 0;
}

题意:

(二分匹配基础题)

每个奶牛有自己喜欢去的谷仓,然后问最多有多少奶牛到了自己喜欢的谷仓;

思路:

把牛和谷仓作为二分图,跑一下匈牙利

#include<iostream>
#include<cstdio>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 210 int cx[250];
int cy[250];
bool vis[250];
int ma[250][250];
int n,m; int fun(int x)
{
for(int i=1;i<=m;i++)
{
if(!vis[i]&&ma[x][i])
{
vis[i]=1;
if(cy[i]==-1||fun(cy[i]))
{
cx[x]=i;
cy[i]=x;
return 1;
}
}
}
return 0;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
int si;
memset(ma,0,sizeof(ma));
for(int i=1;i<=n;i++)
{
scanf("%d",&si);
for(int j=1;j<=si;j++)
{
int x;
scanf("%d",&x);
ma[i][x]=1;
}
}
int ans=0;
memset(cx,-1,sizeof(cx));
memset(cy,-1,sizeof(cy));
for(int i=1;i<=n;i++)
{
if(cx[i]==-1)
{
memset(vis,0,sizeof(vis));
if(fun(i))
{
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}

poj2239 poj1274【二分匹配】的更多相关文章

  1. poj1274 二分匹配

    今天复习二分匹配,A 了一道模板题. 二分匹配需要理解增广路的寻找.用dfs来更新最大匹配.注意一些点:赋初值:愚蠢地把==写成了= ; 然后match的记值;每个点都要重新走一遍. #include ...

  2. POJ2239简单二分匹配

    题意:       一周有7天,每天可以上12节课,现在给你每科课的上课时间,问你一周最多可以上几科课,一科课只要上一节就行了. 思路:       简单题目,直接二分就行了,好久没写二分匹配了,练习 ...

  3. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

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

  4. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  5. BZOJ 1189 二分匹配 || 最大流

    1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1155  Solved: 420[Submi ...

  6. Kingdom of Obsession---hdu5943(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943 题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n ...

  7. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  8. [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  9. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

随机推荐

  1. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  2. Intel processor brand names-Xeon,Core,Pentium,Celeron----Celeron

    http://en.wikipedia.org/wiki/Celeron Celeron From Wikipedia, the free encyclopedia     Celeron Produ ...

  3. A new session could not be created. (Original error: Requested a new session but one was in progress) )错误解决办法

    z在desiredCapabilities里新增这俩居然fix了问题,原因暂时不得而知: capabilities.setCapability("unicodeKeyboard", ...

  4. node.js内存泄露问题记录

    先说一下.事情的来龙去脉. 公司开发一款游戏棋牌游戏,服务端的开发是IO密集型,开发的时候,考虑过使用python,java,node.js. 终于选择了node.js(node.js宣传的杀手功能. ...

  5. Tomcat 安装与配置规范

    Tomcat 安装 演示版本:8.5.32 安装版 JDK推荐版本:jdk1.8 下载地址:https://tomcat.apache.org/download-80.cgi 安装教程 注意:tomc ...

  6. 【bzoj1433】[ZJOI2009]假期的宿舍

    按要求连边,跑匈牙利 #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...

  7. 一条数据的HBase之旅,简明HBase入门教程-开篇

    常见的HBase新手问题: 什么样的数据适合用HBase来存储? 既然HBase也是一个数据库,能否用它将现有系统中昂贵的Oracle替换掉? 存放于HBase中的数据记录,为何不直接存放于HDFS之 ...

  8. DOM操作三

    1.以一个对象的x和y属性的方式返回滚动条的偏移量 function getScrollOffsets(w){ //使用指定的窗口,如果不带参数则使用当前窗口 w= w || window; //除了 ...

  9. 关于static和const

    先谈一下static, 它是一个存储修饰变量.被static修饰的变量存储在静态数据区,只初始化一次,保持数据的持久性.被static修饰的变量和函数有一个共同点是对其他的源文件不可见.被static ...

  10. button在firefox 和 ie 下的问题

    最近做了一个关于数据库管理的项目,因为不用考虑ie9以下的兼容性,所以一股脑的写完啦,到测试的时候发现了一个bug IE和火狐下有个模块关闭按钮的hover没有反应,ie不行就算了,火狐怎么也不行?我 ...