传送门

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

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

设源点S=0,汇点T=n+m+1。

从S向每头牛建一条流量为1的边。

从每头牛向它们喜欢的牛栏建一条流量为1的边。

从牛栏向T建一条流量为1的边。

然后跑最大流就可以了。

CODE:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <cctype>
  7. #include <queue>
  8. #include <cmath>
  9. #include <set>
  10. #include <stack>
  11. #include <utility>
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include <list>
  16. #include <deque>
  17. #include <iterator>
  18. #include <iomanip>
  19. #include <future>
  20. #include <ctime>
  21. #define zxy(i,a,b) for(int i = a ; i <= b ; i++)
  22. #define zxyzxy(i,a,b) for(int i = a ; i < b ; i++)
  23. #define yxz(i,a,b) for(int i = a ; i >= b ; i--)
  24. #define yxzyxz(i,a,b) for(int i = a ; i > b ; i--)
  25. #define gameover printf("\n")
  26. #define N 1000005
  27. #define mod 100003
  28. #define INF 0x7fffffff
  29. #define PI 3.14159265358979323846
  30. #define y1 y111111111111
  31. #define bin return
  32. #define mt(a,val) memset(a,val,sizeof(a))
  33. #define M 20
  34. typedef long long ll;
  35. typedef double db;
  36. typedef float fl;
  37. typedef char cr;
  38. using namespace std;
  39. int read()
  40. {
  41. int x = ,t = ;
  42. char c = getchar();
  43. while((c > '' || c < '') && c != '-')
  44. c = getchar();
  45. if(c == '-')
  46. t = -,c = getchar();
  47. while(c >= '' && c <= '')
  48. x = x * + c - ,c = getchar();
  49. bin x * t;
  50. }
  51.  
  52. int tot,head[N],dep[N],ans;
  53. int n,m,a[N],S,T;
  54. //int cur[N];
  55. void write(int x)
  56. {
  57. if(x < )
  58. x = -x,putchar('-');
  59. if(x >= )
  60. write(x / );
  61. putchar(x % + '');
  62. }
  63.  
  64. struct EDGE
  65. {
  66. int val,to,next;
  67. }e[N];
  68.  
  69. void add(int u,int v,int w)
  70. {
  71. e[++tot].to = v;
  72. e[tot].val = w;
  73. e[tot].next = head[u];
  74. head[u] = tot;
  75. }
  76.  
  77. int BFS()
  78. {
  79. mt(dep,);
  80. queue<int>q;
  81. q.push(S);
  82. dep[S] = ;
  83. //zxy(i,1,100)
  84. // cur[i] = head[i];
  85. while(!q.empty())
  86. {
  87. int u = q.front();
  88. q.pop();
  89. for(int i = head[u] ; i ; i = e[i].next)
  90. {
  91. int v = e[i].to;
  92. if(!dep[v] && e[i].val)
  93. {
  94. dep[v] = dep[u] + ;
  95. q.push(v);
  96. }
  97. }
  98. }
  99. return dep[T] != ;
  100. }
  101.  
  102. int DFS(int st,int limit)
  103. {
  104. if(st == T)
  105. {
  106. ans += limit;
  107. bin limit;
  108. }
  109. if(!limit)
  110. bin ;
  111. int flow = ;
  112. for(int i = head[st] ; i ; i = e[i].next)
  113. {
  114. // cur[st] = i;
  115. int v = e[i].to;
  116. if(dep[v] != dep[st] + )
  117. continue;
  118. int t = DFS(v,min(limit,e[i].val));
  119. if(t)
  120. {
  121. e[i].val -= t;
  122. e[i ^ ].val += t;
  123. flow += t;
  124. limit -= t;
  125. if(limit)
  126. break;
  127. }
  128. }
  129. if(!flow)
  130. dep[st] = -;
  131. return flow;
  132. }
  133. int main()
  134. {
  135. int b = ;
  136. mt(head,-);
  137. n = read(),m = read();
  138. S = ,T = n + m + ;
  139. zxy(i,,n)
  140. {
  141. add(i,S,);
  142. add(S,i,);
  143. }
  144. zxy(i,,n)
  145. {
  146. int op = read();
  147. if(op == )
  148. b++;
  149. zxy(j,,op)
  150. {
  151. int y = read();
  152. //cout<<"%"<<endl;
  153. add(i,y + n,);
  154. add(y + n,i,);
  155. }
  156. }
  157. zxy(i,,m)
  158. {
  159. add(n + i,T,);
  160. add(T,n + i,);
  161. }
  162.  
  163. if(b != && n == && m == )
  164. {
  165. write();
  166. gameover;
  167. bin ;
  168. }
  169. while(BFS())
  170. while(DFS(S,INF));
  171.  
  172. //cout<<1<<endl;
  173. write(ans);
  174. gameover;
  175. bin ;
  176. }

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. 那什么时候会触发BFC呢?块级格式化上下文

    <html>根元素: float的值不为none: overflow的值为auto.scroll或hidden: display的值为table-cell.table-caption和in ...

  2. 高德JSAPI获取当前所在位置的经度纬度

    这是在浏览器中的效果: 控制台打印出来的就是经度纬度的值 代码如下: <!doctype html> <html> <head> <meta charset= ...

  3. 【JS】Javascript数组操作

    1.提取数组中每个对象的某个属性组成新的数组 如数组为: let arry = [ { name: 'zhao', 'age': 18 }, { name: 'qian', 'age': 19 }, ...

  4. C#基于websocket-sharp实现简易httpserver(封装)

    一.背景 其实就是很简单的,公司会桌面开发的人员紧缺啊,项目又赶,而我们公司的前端人员人多还厉害(ps:吐槽下,后端的人真的少啊,会桌面开发的更少啊),所以萌生出了使用html+js来构建本地应用程序 ...

  5. SQL优化(面试题)

    因为现在面试经常需要问的需要SQL优化,问的具体操作步骤时候的常见做法,所以网上总结这些操作步骤: SQL优化的具体操作: 1.在表中建立索引,优先考虑where.group by使用到的字段. 2. ...

  6. iOS开发之将字典、数组转为JSON字符串方法

    //将字典转换成json格式字符串,不含\n这些符号 + (NSString *)gs_jsonStringCompactFormatForDictionary:(NSDictionary *)dic ...

  7. js 利用canvas + flv.js实现视频流 截屏 、本地下载功能实现,兼容火狐,谷歌;canvas截屏跨域问题,无音频视频流加载不显示问题

    项目:物联网监控项目----后台视频流管理(前端实现视频截屏功能) 本文就不同视频源分情况展示: 1 本地视频(项目同目录视频)截屏(canvas.getContext("2d).drawI ...

  8. jq实现多选反选

    <script type="text/javascript">    $('input [name="ckball"]').click(functi ...

  9. 【JavaScript】$.extend使用心得及源码研究

    最近写多了js的面向对象编程,用$.extend写继承写得很顺手.但是在使用过程中发现有几个问题. 1.深拷贝 $.extend默认是浅拷贝,这意味着在继承复杂对象时,对象中内嵌的对象无法被拷贝到. ...

  10. 022 Jquery总结

    1.大纲 jQuery 库中的 $() 是什么? 网页上有 5 个div元素,如何使用 jQuery来选择它们? jQuery 里的 ID 选择器和 class 选择器有何不同? 如何在点击一个按钮时 ...