洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall
原题链接
二分图最大匹配板子。
每个奶牛向它愿意去的牛棚连边,跑二分图最大匹配即可。
这里我用的是匈牙利算法。
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 410;
const int M = N * N;
int fi[N], ne[M], di[M], mtc[N], l;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
bool dfs(int x)
{
int i, y;
for (i = fi[x]; i; i = ne[i])
if (!v[y = di[i]])
{
v[y] = 1;
if (!mtc[y] || dfs(mtc[y]))
{
mtc[y] = x;
return true;
}
}
return false;
}
int main()
{
int i, j, n, m, x, s = 0;
n = re();
re();
for (i = 1; i <= n; i++)
{
m = re();
for (j = 1; j <= m; j++)
{
x = re();
add(i, x + n);
}
}
for (i = 1; i <= n; i++)
{
memset(v, 0, sizeof(v));
if (dfs(i))
s++;
}
printf("%d", s);
return 0;
}
洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall的更多相关文章
- 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall
[题解] 其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了. #include<cstdio> #include<algorithm> #define N 1010 #d ...
- 洛谷——P1894 [USACO4.2]完美的牛栏The Perfect Stall
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷 P1894 [USACO4.2]完美的牛栏The Perfect Stall
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall
题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall题解
题目 二分图最大匹配问题 cow数组标现在牛栏里的牛是几号牛 每次寻找都要清空vis数组 如果可行有两种情况 1.这个牛栏里没有牛 2.这个牛栏里的牛可以到别的牛栏去 根据此递归即可 Code: #i ...
- 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 农夫约翰上个 ...
- [USACO4.2]完美的牛栏The Perfect Stall
题目:USACO Training 4.2(在官网上提交需加文件输入输出).洛谷P1894. 题目大意:有n头奶牛m个牛栏,每头牛只会在自己喜欢的牛栏里产奶,问一次最多有多少奶牛能产奶. 解题思路:二 ...
- 题解 P1894 【[USACO4.2]完美的牛栏The Perfect Stall】
题面 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
随机推荐
- CSS 选用字体
font-family CSS的font-family属性可以指定(选择)字体. 语法: 1 font-family: Gill Sans Extrabold, sans-serif; font-fa ...
- HTTP是用来做什么的
(一)HTTP协议介绍 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目 ...
- nginx反向代理:两个域名指向不同web服务端口
一台服务器上安装了zabbix服务和jumpserver服务,两个域名zabbix.xxxx.xxxx和jumserver.xxx.xxxx 一.编辑/etc/nginx/conf.d目录下nginx ...
- html实现导航栏效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- cekephp 事务处理
$sw = $this->Orders->getDataSource(); $sw->begin(); $this->Orders->commit() $this-> ...
- Application类
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; usin ...
- http4e eclipse plugin 插件介绍
感谢作者的分享: http://blog.csdn.net/wiker_yong/article/details/10066905 以及作者的破解jar.目前看网站留言说已经git了. 官网链接地址: ...
- day28 网络协议OSI 及socket模块
反射都是操作对象中的__dict__的内容 class Student: def study(self): print("正在学习....") stu = Student() if ...
- GsonFormat的使用 (转)
一.Android Studio快速添加Gson 具体操作: 1.File->Project Structure: 2.app->Dependencies->&qu ...
- 【scrapy】爬虫中报Forbidden by robots.txt
需要在setting.py里找到ROBOTSTXT_OBEY并设为false 来源:https://blog.csdn.net/yimingsilence/article/details/521197 ...