洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall

【题解】
其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了。
#include<cstdio>
#include<algorithm>
#define N 1010
#define rg register
using namespace std;
int n,m,E,ans,T,tot,last[N],v[N],from[N];
struct edge{
int to,pre;
}e[N*N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
int dfs(int x){
for(rg int i=last[x],to;i;i=e[i].pre) if(v[to=e[i].to]!=T){
v[to]=T;
if(!from[to]||dfs(from[to])){
from[to]=x;
return ;
}
}
return ;
}
int main(){
n=read(); m=read();
for(rg int i=;i<=n;i++){
int num=read();
for(rg int j=;j<=num;j++){
int v=read();
e[++tot]=(edge){v,last[i]};last[i]=tot;
}
}
for(rg int i=;i<=n;i++) ++T,ans+=dfs(i);
printf("%d\n",ans);
return ;
}
洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall的更多相关文章
- 洛谷1894 [USACO4.2]完美的牛栏The Perfect Stall
原题链接 二分图最大匹配板子. 每个奶牛向它愿意去的牛棚连边,跑二分图最大匹配即可. 这里我用的是匈牙利算法. #include<cstdio> #include<cstring&g ...
- 洛谷——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】
题面 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
随机推荐
- 使用nginx搭建媒体点播服务器
使用nginx搭建媒体点播服务器 最新由于兴趣,对ubuntu和安卓上的视频点播直播等应用比较感兴趣,所以在vmware的虚拟机里面搭建了一个视频点播网站,参考了fengzhanhai的文章Nginx ...
- Redis: Redis on Windows Setup
ylbtech-Redis: Redis on Windows Setup 1.返回顶部 1. 2. 3. 4. 5. 6. 7. 8. 9. 2.返回顶部 3.返回顶部 4.返回顶部 5 ...
- php 批量检测bom头,去除bom头工具
<?php //有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码 if (isset ( $_GET ['dir'] )) { //confi ...
- C#+ItextSharp 查看pdf文件页面尺寸
1# Nuget下载itextSharp,下载到本地 pm>Install-Package iTextSharp -Version 5.5.10 2# 引用dll,添加命名空间 using iT ...
- Invalid default value for 'create_date' timestamp field
创建表的语句中有这么一句 `create_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', 1 之后就报了这个错误. That is be ...
- spring 异常处理
1. 实现接口 HandlerExceptionResolver 捕获异常 2.@ExceptionHandler 在方法添加注解,捕获本地controller异常 3.@ControllerAdvi ...
- ACM_发工资(简单贪心)
发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...
- 234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 详见:https://leetcode.com/problems/palindrome-linked- ...
- C:\Windows\System32\drivers\etc\hosts文件显示
attrib -s -h C:\Windows\System32\drivers\etc\hosts
- 联想 S5 Pro(L78041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.123
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...