两题二分图匹配的题:

1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求。

2.给你学生数和课程数,以及学生上的课,如果可以做到每个学生代表不同的课程并且所有的课程都被代表输出"YES“(学生能代表一门课当且仅当他上过)。

1.POJ 1274 The Perfect Stall

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

和上一题过山车一样,也是二分图匹配的。

水题。

#include<cstdio>
#include<cstring>
const int MAXN=200+10;
int head[MAXN],len,res[MAXN];
bool vis[MAXN];
struct edge
{
int to,next;
}e[MAXN*MAXN];
void add(int from,int to)
{
e[len].to=to;
e[len].next=head[from];
head[from]=len++;
}
bool find(int a)
{
for(int i=head[a];i!=-1;i=e[i].next)
{
int id=e[i].to;
if(!vis[id])
{
vis[id]=true;
if(res[id]==0 || find(res[id]))
{
res[id]=a;
return true;
}
}
}
return false;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));
len=0;
memset(res,0,sizeof(res)); for(int i=1;i<=n;i++)
{
int k,to;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
scanf("%d",&to);
add(i,to);
}
} int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i)) ans++;
}
printf("%d\n",ans);
} return 0;
}

2.POJ 1469 COURSES(zoj 1140)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=140

http://poj.org/problem?id=1469

上面的代码改改就过了。。。

#include<cstdio>
#include<cstring>
const int MAXN=300+10;
int head[MAXN],len,res[MAXN];
bool vis[MAXN];
struct edge
{
int to,next;
}e[MAXN*MAXN];
void add(int from,int to)
{
e[len].to=to;
e[len].next=head[from];
head[from]=len++;
}
bool find(int a)
{
for(int i=head[a];i!=-1;i=e[i].next)
{
int id=e[i].to;
if(!vis[id])
{
vis[id]=true;
if(res[id]==0 || find(res[id]))
{
res[id]=a;
return true;
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int p,n;
scanf("%d%d",&p,&n);
memset(head,-1,sizeof(head));
len=0;
memset(res,0,sizeof(res)); for(int i=1;i<=p;i++)
{
int k,to;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
scanf("%d",&to);
add(i,to);
}
} int ans=0;
for(int i=1;i<=p;i++)
{
memset(vis,0,sizeof(vis));
if(find(i)) ans++;
}
if(ans==p)
puts("YES");
else
puts("NO");
}
return 0;
}

POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配的更多相关文章

  1. Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)

    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...

  2. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

  3. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...

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

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

  5. POJ 1274 The Perfect Stall (二分图匹配)

    [题目链接] http://poj.org/problem?id=1274 [题目大意] 给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛, 问最多可以满足几头奶牛 [题解] 奶牛和喜欢的草棚连线 ...

  6. poj 1274 The Perfect Stall【匈牙利算法模板题】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20874   Accepted: 942 ...

  7. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  8. poj —— 1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26274   Accepted: 116 ...

  9. [题解]poj 1274 The Perfect Stall(网络流)

    二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...

随机推荐

  1. 03013_JDBC工具类

    1.“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils.提供获取连接对象的方法,从而达到代码的重复利用. 2.该工具类提供方法:public static C ...

  2. Unity容器实现自动注册

    如何创建Unity容器? 首先NuGet搜索Unity, 该示例中使用的版本为4.0.1 新建控制台程序 示例中使用常规操作, 创建一个IPay接口, 分别有两个实现类: ApplePay.Huawe ...

  3. OpenCASCADE解非线性方程组

    OpenCASCADE解非线性方程组 eryar@163.com Abstract. 在科学技术领域里常常提出求解非线性方程组的问题,例如,用非线性函数拟合实验数据问题.非线性网络问题.几何上的曲线曲 ...

  4. html元素的分类有哪些?

    今天零度给大家讲一下基本的html元素分类: HTML元素的分类其实主要有两种元素构成——块级元素和内联元素. html元素的分类有块级元素和行内元素 一.块级元素(block)的特点: 1.总是在新 ...

  5. 【基础篇】EditText的一些属性设置

    设置EditText的背景颜色  private test_editText=null; test_editText= (EditText) findViewById(R.id.EditTextInp ...

  6. LuoguP2762 太空飞行计划问题(最大权闭合子图,最小割)

    题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的全部仪器的 ...

  7. vsphere client和vsphere web client的区别

    vsphere client是一个运行在windows桌面上的客户端,在linux环境下无法运行,在vsphere5.0以后,VMware在逐渐弱化vsphere client的作用,现在很多高级功能 ...

  8. logname---显示用户名称

    logname命令用来显示用户名称.

  9. JS-网页中分页栏

    原理 三部分 我给分页栏分成了3部分 上一页:调用prePage()函数 下一页:调用nextPage()函数 带有数字标识的部,调用skipPage()函数 prePage函数 function p ...

  10. [Python] Python's namedtuples can be a great alternative to defining a class manually

    # Why Python is Great: Namedtuples # Using namedtuple is way shorter than # defining a class manuall ...