两题二分图匹配的题:

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. PatentTips - System and method to deprivilege components of a virtual machine monitor

    BACKGROUND INFORMATION An embodiment of the present invention relates generally to virtualization pl ...

  2. cogs 49. 跳马问题

    49. 跳马问题 ★   输入文件:horse.in   输出文件:horse.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 有一只中国象棋中的 “ 马 ” ,在半张 ...

  3. Starting nagios:This account is currently not available.

    解决方式: 又一次安装php 再重新启动apache 再启动nagios 再訪问:http://ip/nagios 我的问题就是 解决的.

  4. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...

  5. Fragment-管理Fragment2

    上一篇,给大家讲了有关Fragment管理的几个函数,即add,replace,remove,这节再讲讲其它函数,然后再给大家看一个系统BUG. 一.hide().show() 1.基本使用 这两个函 ...

  6. nice---进程优先级

    在当前程序运行优先级基础之上调整指定值得到新的程序运行优先级,用新的程序运行优先级运行命令行"command [arguments...]".优先级的范围为-20 - 19 等40 ...

  7. Hyperic

    https://my.oschina.net/hyperichq/blog/525590

  8. HDU——T 1573 X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 Time Limit: 1000/1000 MS (Java/Others)    Memory Limi ...

  9. GCC中-fpic解惑(转载)

    参考: 1.<3.18 Options for Code Generation Conventions>2.<Options for Linking>3.<GCC -fP ...

  10. cocos2dx-js学习笔记(一)环境搭建

    本人眼下的学习方向是cocos2dx+js的开发方式,开发调试使用webstrom和火狐浏览器,调试完毕的项目使用cocos2dx+jsb的方式编译到PC或android设备执行.主要时间用在学习,所 ...