两题二分图匹配的题:

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. 学习——HTML5中事件

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  2. 【转】flex中的labelFunction(combox和dataGrid)

    Flex中,对于显示一个字段,只需要指定对应字段属性给labelField即可,当需要上述所需要的功能的时候就得做个转换了,在Flex的基于List的组件都有一个labelFunction方法能很简单 ...

  3. XTUOJ 1205 Range

    Range Time Limit : 1000 MS Memory Limit : 65536 KB Problem Description For an array, the range funct ...

  4. CodeForcesGym 100502K Train Passengers

    Train Passengers Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForces ...

  5. 可靠的UDP连接 & MTU MSS

    这个网页里面写了: http://blog.csdn.net/plusboy/article/details/1523308 其可靠性必须由上层应用实现.一般都会采用消息重传来实现其可靠性,采用消息重 ...

  6. Greenplum中定义数据库对象之创建与管理模式

    创建与管理模式 概述:DB内组织对象的一种逻辑结构.一个DB内能够有多个模式.在未指定模式时默认放置在public中.能够通过"\dn"方式查看数据库中现有模式. testdw=# ...

  7. hdu 1875 畅通project再续(kruskal算法计算最小生成树)

    畅通project再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. Eclipse中JDK的配置

    window -> preference -> java -> install jres -> add -> standard vm -> 设置好相应的jre ho ...

  9. 63.note.js之 Mongodb在Nodejs上的配置及session会话机制的实现

    转自:https://www.cnblogs.com/alvin_xp/p/4751784.html 1.第一步安装mongodb数据库,这直接官网下载,这里不介绍. 2.也可以使用npm实现直接下载 ...

  10. 73,QT指针数组实战(指针数组与数组指针)

    //指针数组,每一个指针都是一个MainWindow // MainWindow *w[3][4]; // for(int i=0;i<3;i++) // { // for(int j=0;j& ...