洛谷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】
题面 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
随机推荐
- Ros系列_学习一
刚入门ROS,不,没入门,还在门口,这是今天的总结: (一)创建一个工作空间 1.创建一个初始工作空间: mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src cat ...
- 安装 telnet
yum install telnet-server yum install telnet service xinetd restart 查询是否正常启动telnet netstat -tn ...
- linux安装php-redis扩展
wget http://pecl.php.net/get/redis-2.2.5.tgz #解压 tar zxvf redis-2.2.5.tgz #进入安装目录 cd redis-2.2.5 /us ...
- linux基本之一
1.目录棉集 / 根目录,理论上讲系统中的一切都属于他. /bin 存放所有用户都能执行的命令(二进制) /boot 存放启动文件/内核的相关文件,一般独立成为一个分区. /dev 存放物理设备的目录 ...
- div 光标处插入内容
var Manager = { insertHtml: function(html, type) { var lastMemo = document.getElementById("memo ...
- maven 配置 阿里仓库
<mirror><id>nexus-aliyun</id><mirrorOf>central</mirrorOf><name>N ...
- Handling Event
[Handling Event] 1.React events are named using camelCase 2.You must call preventDefault explicitly ...
- oracle 一致读原理
在Oracle数据库中,undo主要有三大作用:提供一致性读(Consistent Read).回滚事务(Rollback Transaction)以及实例恢复(Instance Recovery). ...
- 无法加载DLL"***.dll":找不到指定的模块
加载dll的路径不对. 绝对路径不合适,可以换成相对路径. 比如: 把dll放入bin目录下的debug或者release下,然后就可以直接“test.dll”了.不用加路径了. 注意:路径必须与发布 ...
- 【转】修复关于apache-xampp的问题:Port 443 in use by “vmware-hostd.exe”!
在电脑里装了VMware后,再要装xampp,十有八九就会出现这个问题: 11:23:37 [Apache] Problem detected! 11:23:37 [Apache] ...