//==========================

蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/  转载要声明!

//==========================

判断二分图中某条路是否是唯一的。

网络流做法要加个tarjan

二分图就是再增广看能不能增广

网络流(快但是长)

  1. type
  2. arr=record
  3. toward,next,cap,from:longint;
  4. end;
  5. const
  6. maxn=;
  7. maxm=;
  8. var
  9. edge:array[..maxm]of arr;
  10. first,cur,d,p,gap,num,e,belong,dfn,low:array[..maxn]of longint;
  11. chose:array[..maxn]of boolean;
  12. trie:array[..maxn,'A'..'z'] of longint;
  13. n,esum,s,t,tot,total,peo,time,scc,top:longint;
  14.  
  15. procedure add(j,k,l:longint);
  16. begin
  17. inc(esum);
  18. edge[esum].toward:=k;
  19. edge[esum].next:=first[j];
  20. first[j]:=esum;
  21. edge[esum].from:=j;
  22. edge[esum].cap:=l;
  23. end;
  24.  
  25. procedure addedge(j,k,l:longint);
  26. begin
  27. add(j,k,l);
  28. add(k,j,);
  29. end;
  30.  
  31. function find(ss:string):longint;
  32. var
  33. i,u:longint;
  34. begin
  35. u:=;
  36. for i:= to length(ss) do u:=trie[u][ss[i]];
  37. exit(num[u]);
  38. end;
  39.  
  40. function min(x,y:longint):longint;
  41. begin
  42. if x<y then exit(x);
  43. exit(y);
  44. end;
  45.  
  46. procedure into;
  47. var
  48. i,j,k,m,u,boy,girl:longint;
  49. ss:string;
  50. begin
  51. esum:=-;
  52. tot:=;
  53. total:=;
  54. peo:=;
  55. fillchar(first,sizeof(first),);
  56. readln(n);
  57. s:=n<<+;
  58. t:=n<<+;
  59. tot:=n<<+;
  60. for i:= to n do begin
  61.  
  62. readln(ss);
  63. ss:=ss+' ';
  64. u:=;
  65. j:=;
  66. while (ss[j]<>' ') do begin
  67. if trie[u][ss[j]]= then begin
  68. inc(total);
  69. trie[u][ss[j]]:=total;
  70. end;
  71. u:=trie[u][ss[j]];
  72. inc(j);
  73. end;
  74. inc(peo);
  75. num[u]:=peo;
  76. girl:=peo;
  77.  
  78. inc(j);
  79. u:=;
  80. while (ss[j]<>' ') do begin
  81. if trie[u][ss[j]]= then begin
  82. inc(total);
  83. trie[u][ss[j]]:=total;
  84. end;
  85. u:=trie[u][ss[j]];
  86. inc(j);
  87. end;
  88. inc(peo);
  89. num[u]:=peo;
  90. boy:=peo;
  91.  
  92. e[i]:=esum+;
  93. addedge(girl,boy,);
  94. addedge(s,girl,);
  95. addedge(boy,t,);
  96.  
  97. end;
  98. readln(m);
  99. while m> do begin
  100. dec(m);
  101. readln(ss);
  102. i:=pos(' ',ss);
  103. j:=find(copy(ss,,i-));
  104. k:=find(copy(ss,i+,length(ss)-i));
  105. addedge(j,k,);
  106. end;
  107. end;
  108.  
  109. function sap(x,flow:longint):longint;
  110. var
  111. now,more,i,too:longint;
  112. begin
  113. if x=t then exit(flow);
  114. now:=;
  115. i:=cur[x];
  116. while i>= do begin
  117. too:=edge[i].toward;
  118. if (d[x]=d[too]+) and (edge[i].cap>) then begin
  119. more:=sap(too,min(flow-now,edge[i].cap));
  120. dec(edge[i].cap,more);
  121. inc(edge[i xor ].cap,more);
  122. inc(now,more);
  123. cur[x]:=i;
  124. if flow=now then exit(flow);
  125. end;
  126. i:=edge[i].next;
  127. end;
  128. dec(gap[d[x]]);
  129. if gap[d[x]]= then d[s]:=tot;
  130. inc(d[x]);
  131. inc(gap[d[x]]);
  132. cur[x]:=first[x];
  133. exit(now);
  134. end;
  135.  
  136. procedure maxflow;
  137. var
  138. i:longint;
  139. begin
  140. fillchar(gap,sizeof(gap),);
  141. fillchar(d,sizeof(d),);
  142. gap[]:=tot;
  143. for i:= to tot do cur[i]:=first[i];
  144. while d[s]<tot do sap(s,maxlongint);
  145. end;
  146.  
  147. procedure tarjan(x:longint);
  148. var
  149. i,j,too:longint;
  150. begin
  151. inc(time);
  152. dfn[x]:=time;
  153. low[x]:=time;
  154. inc(top);
  155. p[top]:=x;
  156. chose[x]:=true;
  157. i:=first[x];
  158. while i>= do begin
  159. if edge[i].cap> then begin
  160. too:=edge[i].toward;
  161. if dfn[too]= then begin
  162. tarjan(too);
  163. low[x]:=min(low[x],low[too]);
  164. end
  165. else
  166. if chose[too] then
  167. low[x]:=min(low[x],low[too]);
  168. end;
  169. i:=edge[i].next;
  170. end;
  171. if low[x]=dfn[x] then begin
  172. inc(scc);
  173. repeat
  174. j:=p[top];
  175. dec(top);
  176. chose[j]:=false;
  177. belong[j]:=scc;
  178. until j=x;
  179. end;
  180. end;
  181.  
  182. procedure work;
  183. var
  184. head,tail,i,j,k,l,x,too:longint;
  185. flag:boolean;
  186. begin
  187. time:=;
  188. fillchar(chose,sizeof(chose),false);
  189. fillchar(dfn,sizeof(dfn),);
  190. top:=;
  191. for i:= to tot do
  192. if dfn[i]= then tarjan(i);
  193. for i:= to n do begin
  194. j:=e[i];
  195. k:=edge[j].from;
  196. l:=edge[j].toward;
  197. if (belong[k]=belong[l]) or (edge[j].cap>) then writeln('Unsafe')
  198. else writeln('Safe');
  199. end;
  200. end;
  201.  
  202. begin
  203. into;
  204. maxflow;
  205. work;
  206. end.

匈牙利版

  1. type
  2. arr=record
  3. toward,next,from:longint;
  4. flag:boolean;
  5. end;
  6. const
  7. maxn=;
  8. maxm=;
  9. var
  10. edge:array[..maxm]of arr;
  11. first,cur,d,p,gap,num,e,match,matche:array[..maxn]of longint;
  12. chose:array[..maxn]of boolean;
  13. trie:array[..maxn,'A'..'z'] of longint;
  14. n,esum,s,t,tot,total,peo,time,scc,top:longint;
  15.  
  16. procedure addedge(j,k:longint);
  17. begin
  18. inc(esum);
  19. edge[esum].from:=j;
  20. edge[esum].toward:=k;
  21. edge[esum].next:=first[j];
  22. edge[esum].flag:=true;
  23. first[j]:=esum;
  24. end;
  25.  
  26. function find(ss:string):longint;
  27. var
  28. i,u:longint;
  29. begin
  30. u:=;
  31. for i:= to length(ss) do u:=trie[u][ss[i]];
  32. exit(num[u]);
  33. end;
  34.  
  35. procedure into;
  36. var
  37. i,j,k,m,u,boy,girl:longint;
  38. ss:string;
  39. begin
  40. esum:=;
  41. tot:=;
  42. total:=;
  43. peo:=;
  44. fillchar(first,sizeof(first),);
  45. readln(n);
  46. for i:= to n do begin
  47.  
  48. readln(ss);
  49. ss:=ss+' ';
  50. u:=;
  51. j:=;
  52. while (ss[j]<>' ') do begin
  53. if trie[u][ss[j]]= then begin
  54. inc(total);
  55. trie[u][ss[j]]:=total;
  56. end;
  57. u:=trie[u][ss[j]];
  58. inc(j);
  59. end;
  60. inc(peo);
  61. num[u]:=peo;
  62. girl:=peo;
  63.  
  64. inc(j);
  65. u:=;
  66. while (ss[j]<>' ') do begin
  67. if trie[u][ss[j]]= then begin
  68. inc(total);
  69. trie[u][ss[j]]:=total;
  70. end;
  71. u:=trie[u][ss[j]];
  72. inc(j);
  73. end;
  74. inc(peo);
  75. num[u]:=peo;
  76. boy:=peo;
  77.  
  78. e[i]:=esum+;
  79. addedge(girl,boy);
  80.  
  81. end;
  82. readln(m);
  83. while m> do begin
  84. dec(m);
  85. readln(ss);
  86. i:=pos(' ',ss);
  87. j:=find(copy(ss,,i-));
  88. k:=find(copy(ss,i+,length(ss)-i));
  89. addedge(j,k);
  90. end;
  91. end;
  92.  
  93. function dfs(x:longint):boolean;
  94. var
  95. i,too:longint;
  96. begin
  97. i:=first[x];
  98. while i> do begin
  99. too:=edge[i].toward;
  100. if edge[i].flag and chose[too] then begin
  101. chose[too]:=false;
  102. if (match[too]=) or dfs(match[too]) then begin
  103. edge[matche[too]].flag:=true;
  104. matche[too]:=i;
  105. match[too]:=x;
  106. edge[i].flag:=false;
  107. exit(true);
  108. end;
  109. end;
  110. i:=edge[i].next;
  111. end;
  112. exit(false);
  113. end;
  114.  
  115. procedure work;
  116. var
  117. i,j,boy,girl:longint;
  118. begin
  119. for i:= to n do begin
  120. fillchar(chose,sizeof(chose),true);
  121. dfs(edge[e[i]].from);
  122. end;
  123. //for i:= to n do writeln(match[i<<]);
  124. for i:= to n do begin
  125. fillchar(chose,sizeof(chose),true);
  126. j:=e[i];
  127. if edge[j].flag then begin
  128. writeln('Unsafe');
  129. continue;
  130. end;
  131. boy:=edge[j].toward;
  132. girl:=edge[j].from;
  133. match[boy]:=;
  134. if not dfs(girl) then begin
  135. match[boy]:=girl;
  136. matche[boy]:=j;
  137. writeln('Safe');
  138. end
  139. else writeln('Unsafe');
  140. end;
  141. end;
  142.  
  143. begin
  144. into;
  145. work;
  146. end.

bzoj 2140: 稳定婚姻 (二分图)的更多相关文章

  1. BZOJ 2140 稳定婚姻 ——二分图

    论二分图的可行边与必须边. 考虑用dinic增广之后的图,一些是必要的割边,一些是可行的割边. 我们首先求出一组可行的最大匹配,那么这些变都是可行的. 然后我们求一遍强连通分量. 如果 scc[u]! ...

  2. BZOJ 2140 稳定婚姻

    2140: 稳定婚姻 Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...

  3. 2140: 稳定婚姻 - BZOJ

    Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就 ...

  4. 【BZOJ】2140 稳定婚姻

    [解析]Hash,离散化.Tarjan [分析] 对于每一个名字.首先离散化成编号. 用hash或者其它,反正不要最主要的即可了.否则O(N^2L)会爆掉. 然后请參考:http://www.cnbl ...

  5. BZOJ2140: 稳定婚姻

    题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...

  6. BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)

    2140: 稳定婚姻 Time Limit: 2 Sec  Memory Limit: 259 MBSubmit: 1321  Solved: 652[Submit][Status][Discuss] ...

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  8. 洛谷 P1407 [国家集训队]稳定婚姻 解题报告

    P1407 [国家集训队]稳定婚姻 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...

  9. 【bzoj2140】: 稳定婚姻 图论-tarjan

    [bzoj2140]: 稳定婚姻 哎..都是模板题.. 一眼看过去 哇 二分图哎 然后发现好像并不能匈牙利算法 自己xjb画两张图,发现二分图左向右连配偶的边,然后右向左连交往过的边 然后如果Bi G ...

随机推荐

  1. 理解Python的装饰器

    看Flask文档时候看到关于cache的装饰器,有这么一段代码: def cached(timeout=5 * 60, key=’view/%s’): def decorator(f): @wraps ...

  2. Visual Studio 起始页中不显示最近使用的项目的解决办法

    将 HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/NoRecentDocsHistory的 ...

  3. zipaligin的使用介绍

    近来一直在做APK反编译和重编译的工作,针对一些apk需要放入一些相应的文件,(当然这里不涉及非法盈利,都是有合约的),在对一些包打包以后,发现可以通过一个叫做zipalign的工具进行优化,对于这个 ...

  4. photoshop cc 2018安装破解教程(破解补丁,亲测,绝对可用)

    破解步骤说明:下载地址百度网盘,https://pan.baidu.com/s/1cWtpUesl2fms3tFwEC0MiQ 1.右键解压Adobe Photoshop CC 2018 64位这个文 ...

  5. Ping隧道

    1.研究原因: 校园内网的探索,校内电子图书馆资源的利用,认证校园网 2.目的: 内网服务器:在一台因防火墙等原因仅限icmp数据通过的 公网服务器 : 建立icmp 隧道链接,  并在公网服务器上进 ...

  6. 180605-Linux下Crontab实现定时任务

    Linux下Crontab实现定时任务 基于Hexo搭建的个人博客,是一种静态博客页面,每次新增博文或者修改,都需要重新的编译并发布到Github,这样操作就有点蛋疼了,一个想法就自然而然的来了,能不 ...

  7. web自动化原理揭秘

    做过两年自动化测试的小伙伴说web自动化测试真的不难,无非就是一些浏览器操作,页面元素操作,常规的情况很容易处理,再学一学特殊元素的处理,基本就能应付项目的测试了. 这个话倒没错,但是真正要学好自动化 ...

  8. Linux命令应用大词典-第28章 硬件管理

    28.1 lscpu:显示有关CPU架构的信息 28.2 nproc:显示当前进程可用的CPU数目 28.3 chcpu:配置CPU

  9. 微信小程序入门学习之事件 事件对象 冒泡非冒泡事件(1)

    这关于事件的学习,可以自己复制到微信开发者工具上自己运行试试. 首先这里有两个文件.js 和.wxml 文件 首先给出.js文件下代码 // pages/news/news.js Page({ /** ...

  10. 孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1

    孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1 (完整学习过程屏幕记录视频地址在文末) 要模拟进行浏览器操作,只用requests是不行的,因此今天了解到有专门的解决方案 ...