两题二分图匹配的题:

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. 洛谷 P1581 A+B Problem(升级版)

    P1581 A+B Problem(升级版) 题目背景 小明这在写作业,其中有一道A+B Problem ,他想啊想啊想,就是想不出来,于是就找到了会编程的你...... 题目描述 这里的A+B是很奇 ...

  2. 自考之SDT

    软件开发工具(Soft Development Tools)是一本让程序猿了解自己自己所使用工具的书,作为一个刚刚接触编程的小菜鸟.计划工具.分析工具.设计工具.尽管用的都不是非常多,但也有一个概念了 ...

  3. mysql异常Lock wait timeout exceeded; try restarting transaction

    mysql中使用update语句更新数据报错: Lock wait timeout exceeded; try restarting transaction. 这是由于你要更新的表的锁在其它线程手里. ...

  4. 查看activity task相关信息

    可以使用命令 adb shell dumpsys activity 查看的结果如下 ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents ...

  5. 8.ZOrder

    T3LayerZorder.h #pragma once #include "cocos2d.h" USING_NS_CC; class T3LayerZorder:public ...

  6. 暑假集训-二分图,网络流,2-SAT

    匈牙利算法DFS bool dfs(int u){ ; i <= n; i++){ if(a[u][i] && !visit[i]){ visit[i] = true; || d ...

  7. NOI2017整数

    NOI2017 整数 题意: ​ 让你实现两个操作: 1 \(a\) \(b\):将\(x\)加上整数\(a \cdot 2 ^ b\),其中 \(a\)为一个整数,\(b\)为一个非负整数 2 \( ...

  8. telint---切换当前正在运行的Linux系统的运行等级

    telint命令用于切换当前正在运行的Linux系统的运行等级 Send control commands to the init daemon. --help Show this help --no ...

  9. mysql 数据库 存储数据类型

    int 类型的数据  可以在数据库里存成 char字符串类型的数据: 纯数字的字符串 可以在数据库里存储为 int的数据类型.

  10. FZU 1921 栀子花开

    栀子花开 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 19216 ...