传送门

是道绿题???二分图(网络流)不应该是蓝打底???

这题浏览一遍就知道是二分图(网络流)算法喽,二分图代码太短,不想写(←这人???),所以就拿网络流练练手。

设源点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的更多相关文章

  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. 洛谷——P1894 [USACO4.2]完美的牛栏The Perfect Stall

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  3. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

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

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  5. POJ1274 The Perfect Stall[二分图最大匹配]

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  6. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  7. poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16396   Accepted: 750 ...

  8. poj 1274 The Perfect Stall【匈牙利算法模板题】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20874   Accepted: 942 ...

  9. USACO Section 4.2 The Perfect Stall(二分图匹配)

    二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...

随机推荐

  1. 微信小程序开发学习(二)

    一些官方API 总结了一些官方API,便于之后有用时针对性查找(发现官方给了好多好用的API)官方API文档 基础 wx.canIUse:判断小程序的API,回调,参数,组件等是否在当前版本可用,返回 ...

  2. cURL error 60: SSL certificate problem: unable to get local issuer

    github 问题连接 https://github.com/yabacon/paystack-php/wiki/cURL-error-60:-SSL-certificate-problem:-una ...

  3. jsp参数乱码解决

    iframe src引入jsp,?跟着的中文参数会乱码 解决方法: var CJJG=encodeURI(encodeURI(value.data.CJJG));//js里面转码一次 //jsp页面里 ...

  4. Django—视图

    索引 一.URLconf 二.视图 三.HttpReqeust对象 3.1 QueryDict对象 3.2 GET和POST属性 四.HttpResponse对象 4.1 子类JsonResponse ...

  5. iOS UINavigationController Class Translation

    类   UINavigationController 一个容器视图控制器,定义了用于导航分层内容的基于堆栈的方案.   概述   导航控制器是一个在导航界面中,管理一个或多个子视图控制器的容器视图控制 ...

  6. JIRA API 对接

    系统要跟JIRA对接,将本系统数据发送给jira. 开始一头雾水怎么让数据传过去已什么形式存在,是存数据库呢还是怎么显示呢.研究半天发现其实只要将原数据作为json数据提供给jira接口,jira接口 ...

  7. 方法总结:如何实现html页面自动刷新

    使用场景: 1. 页面需要定时刷新,实时加载数据,需要实时查看监控数据(H5中的WebSocket和SSE可以实现局部刷新) 2. 一定时间之后跳转到指定页面(登录注册之类) 3. 前端开发使用伪数据 ...

  8. Oracle体系结构之内存结构(3)

    Oracle内存结构由系统全局区SGA和程序全局区PGA组成. SGA是实例启动的时候分配的Oracle实例中的一个基本成分. PGA是服务器进程启动时分配. 1.系统全局区SGA 系统全局区SGA由 ...

  9. 在Windows上使用Let加密IIS

    在Windows上使用Let加密IIS https://weblog.west-wind.com/posts/2016/Feb/22/Using-Lets-Encrypt-with-IIS-on-Wi ...

  10. pyspark如何遍历broadcast

    因为论文关系要用到pyspark,具体情形如下: 有一个list=['aaa','bbb','ccc','ddd'],然后有一个rdd内数据类型是str,eg:'abcdefg',正常如果是需要筛选数 ...