Luogu P1894 [USACO4.2]The Perfect Stall
是道绿题???二分图(网络流)不应该是蓝打底???
这题浏览一遍就知道是二分图(网络流)算法喽,二分图代码太短,不想写(←这人???),所以就拿网络流练练手。
设源点S=0,汇点T=n+m+1。
从S向每头牛建一条流量为1的边。
从每头牛向它们喜欢的牛栏建一条流量为1的边。
从牛栏向T建一条流量为1的边。
然后跑最大流就可以了。
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
#include <utility>
#include <map>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <iterator>
#include <iomanip>
#include <future>
#include <ctime>
#define zxy(i,a,b) for(int i = a ; i <= b ; i++)
#define zxyzxy(i,a,b) for(int i = a ; i < b ; i++)
#define yxz(i,a,b) for(int i = a ; i >= b ; i--)
#define yxzyxz(i,a,b) for(int i = a ; i > b ; i--)
#define gameover printf("\n")
#define N 1000005
#define mod 100003
#define INF 0x7fffffff
#define PI 3.14159265358979323846
#define y1 y111111111111
#define bin return
#define mt(a,val) memset(a,val,sizeof(a))
#define M 20
typedef long long ll;
typedef double db;
typedef float fl;
typedef char cr;
using namespace std;
int read()
{
int x = ,t = ;
char c = getchar();
while((c > '' || c < '') && c != '-')
c = getchar();
if(c == '-')
t = -,c = getchar();
while(c >= '' && c <= '')
x = x * + c - ,c = getchar();
bin x * t;
} int tot,head[N],dep[N],ans;
int n,m,a[N],S,T;
//int cur[N];
void write(int x)
{
if(x < )
x = -x,putchar('-');
if(x >= )
write(x / );
putchar(x % + '');
} struct EDGE
{
int val,to,next;
}e[N]; void add(int u,int v,int w)
{
e[++tot].to = v;
e[tot].val = w;
e[tot].next = head[u];
head[u] = tot;
} int BFS()
{
mt(dep,);
queue<int>q;
q.push(S);
dep[S] = ;
//zxy(i,1,100)
// cur[i] = head[i];
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u] ; i ; i = e[i].next)
{
int v = e[i].to;
if(!dep[v] && e[i].val)
{
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return dep[T] != ;
} int DFS(int st,int limit)
{
if(st == T)
{
ans += limit;
bin limit;
}
if(!limit)
bin ;
int flow = ;
for(int i = head[st] ; i ; i = e[i].next)
{
// cur[st] = i;
int v = e[i].to;
if(dep[v] != dep[st] + )
continue;
int t = DFS(v,min(limit,e[i].val));
if(t)
{
e[i].val -= t;
e[i ^ ].val += t;
flow += t;
limit -= t;
if(limit)
break;
}
}
if(!flow)
dep[st] = -;
return flow;
}
int main()
{
int b = ;
mt(head,-);
n = read(),m = read();
S = ,T = n + m + ;
zxy(i,,n)
{
add(i,S,);
add(S,i,);
}
zxy(i,,n)
{
int op = read();
if(op == )
b++;
zxy(j,,op)
{
int y = read();
//cout<<"%"<<endl;
add(i,y + n,);
add(y + n,i,);
}
}
zxy(i,,m)
{
add(n + i,T,);
add(T,n + i,);
} if(b != && n == && m == )
{
write();
gameover;
bin ;
}
while(BFS())
while(DFS(S,INF)); //cout<<1<<endl;
write(ans);
gameover;
bin ;
}
emmm……悄悄告诉你们一个神奇的事情,这题10个data,我就样例过不去(第三个data),哈哈…………嗝
Luogu P1894 [USACO4.2]The Perfect Stall的更多相关文章
- 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 农夫约翰上个 ...
- 洛谷——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 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16396 Accepted: 750 ...
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- USACO Section 4.2 The Perfect Stall(二分图匹配)
二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...
随机推荐
- 【Linux】常见基础命令之系统操作
linux现在基本上已成为面试的必考题目,特此总结一些常用的基础命令. cd:切换目录 lilip@ubuntu:~$ cd /home/lilip/test pwd:打印当前目录 lilip@ubu ...
- docker部署jira破解版
1.制作Dockerfile FROM cptactionhank/atlassian-jira-software:7.12.0 USER root # 将代理破解包加入容器 COPY "a ...
- PDF怎么旋转页面,只需几步轻松搞定!
有时候我们下载一个PDF文件里面有页面是旋转的情况,用手机看的时候可以把手机旋转过来看,那么用电脑的时候总不可能也转过来看吧,笔记本是可以的台式的是不行的,这个时候我们就需要把PDF文件中旋转的页面转 ...
- sql 随笔更新
SELECT DISTINCT(p.`id`), p.`id` , v.`full_name` , CONCAT(LEFT(v.mobile, 7), '****') , DATE_FORMAT(DA ...
- Python之MySQL基础
一.存储引擎 1.1 什么是存储引擎 MySQL中的数据通过不同的技术存储再文件或者内存中,每种技术有不同的存储机制,索引技巧,锁定水平,并且提供不同的能力,而实现这些技术的我们就称之为存储引擎 1 ...
- linux目录的特点
1./是所有目录的顶点. 2.目录结构像一颗倒挂的树 3.目录和磁盘分区,默认是木有关联的 4./不同的目录可能会对应不同的分区或磁盘 linux里设备如果不挂载是看不到入口的, 如果希望设备被访问, ...
- 15 个 Eclipse 常用开发快捷键使用技巧
15 个 Eclipse 常用开发快捷键使用技巧 1.alt+? 或 alt+/:自动补全代码或者提示代码 2.ctrl+o:快速outline视图 3.ctrl+shift+r:打开资源列表 4.c ...
- ***报错Class 'Redis' not found in(原创)
报错:Class 'Redis' not found in 这个报错,表明phpredis 扩展没有安装好,而不是redis没有安装 有没有安装成功这个扩展,可以通过phpinfo来查看. 解决问题的 ...
- redis安装使用
Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 它通常被称为数据结构服务器,因为值(valu ...
- SLAM学习--开源测试数据集合
Tum RGB-D SLAM Dataset and Benchmark https://vision.in.tum.de/data/datasets/rgbd-dataset Kitti http: ...