POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题:
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)二分图匹配的更多相关文章
- 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 农夫约翰上个 ...
- poj——1274 The Perfect Stall
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- POJ 1274 The Perfect Stall (二分图匹配)
[题目链接] http://poj.org/problem?id=1274 [题目大意] 给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛, 问最多可以满足几头奶牛 [题解] 奶牛和喜欢的草棚连线 ...
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- poj 1274 The Perfect Stall (二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
- poj —— 1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26274 Accepted: 116 ...
- [题解]poj 1274 The Perfect Stall(网络流)
二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...
随机推荐
- 洛谷 P1705 爱与愁过火
P1705 爱与愁过火 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第三章. 爱与愁大神说这是ta的伤心指数,只不过现在好很多了,翻译只 ...
- 父类与子类的virtual
父类加了virtual,子类不需要加virtual,多余.加了也不会报错. 父类中不是virtual,子类是virtual,那么父类中的不是虚函数,子类及子子类的派生类中该函数才是虚函数
- js 图片轮转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java架构解密——实时动态aop
在上篇博客中个.咱们一起组建了一个容器,里面封装了业务,这样,咱们就将业务和服务的组装放到了client,而client就相当于咱们的开发中使用到的配置文件.大家发现问题了吗?就是我不能动态修改了?业 ...
- JavaWeb-04(BOM&DOM)
JavaWeb-04 JavaWeb-BOM&DOM BOM 一.知识回想 * BOM 概述 * BOM 的各个对象 * window对象 innerHeight,innerWidth doc ...
- Vue v-if v-for v-bind v-on
v-if <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'&q ...
- 2.Maven之(二)Maven生命周期
转自:https://blog.csdn.net/u012152619/article/details/51473404 我们在开发项目的时候,不断地在编译.测试.打包.部署等过程,maven的生命周 ...
- 3.索引与string进行映射实现高效查找
#include <iostream> #include <typeindex>//类型索引 #include <unordered_map>//红黑树 #incl ...
- marquee
marquee: 页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需 ...
- Intent调用系统拍照程序,返回图片太小的问题
之前採用的方式(返回的照片会被压缩,不能达到预期效果): Intent intent = new Intent(); Intent intent_camera = getPackageManager( ...