bzoj 2140: 稳定婚姻 (二分图)
//==========================
蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明!
//==========================
判断二分图中某条路是否是唯一的。
网络流做法要加个tarjan
二分图就是再增广看能不能增广
网络流(快但是长)
- type
- arr=record
- toward,next,cap,from:longint;
- end;
- const
- maxn=;
- maxm=;
- var
- edge:array[..maxm]of arr;
- first,cur,d,p,gap,num,e,belong,dfn,low:array[..maxn]of longint;
- chose:array[..maxn]of boolean;
- trie:array[..maxn,'A'..'z'] of longint;
- n,esum,s,t,tot,total,peo,time,scc,top:longint;
- procedure add(j,k,l:longint);
- begin
- inc(esum);
- edge[esum].toward:=k;
- edge[esum].next:=first[j];
- first[j]:=esum;
- edge[esum].from:=j;
- edge[esum].cap:=l;
- end;
- procedure addedge(j,k,l:longint);
- begin
- add(j,k,l);
- add(k,j,);
- end;
- function find(ss:string):longint;
- var
- i,u:longint;
- begin
- u:=;
- for i:= to length(ss) do u:=trie[u][ss[i]];
- exit(num[u]);
- end;
- function min(x,y:longint):longint;
- begin
- if x<y then exit(x);
- exit(y);
- end;
- procedure into;
- var
- i,j,k,m,u,boy,girl:longint;
- ss:string;
- begin
- esum:=-;
- tot:=;
- total:=;
- peo:=;
- fillchar(first,sizeof(first),);
- readln(n);
- s:=n<<+;
- t:=n<<+;
- tot:=n<<+;
- for i:= to n do begin
- readln(ss);
- ss:=ss+' ';
- u:=;
- j:=;
- while (ss[j]<>' ') do begin
- if trie[u][ss[j]]= then begin
- inc(total);
- trie[u][ss[j]]:=total;
- end;
- u:=trie[u][ss[j]];
- inc(j);
- end;
- inc(peo);
- num[u]:=peo;
- girl:=peo;
- inc(j);
- u:=;
- while (ss[j]<>' ') do begin
- if trie[u][ss[j]]= then begin
- inc(total);
- trie[u][ss[j]]:=total;
- end;
- u:=trie[u][ss[j]];
- inc(j);
- end;
- inc(peo);
- num[u]:=peo;
- boy:=peo;
- e[i]:=esum+;
- addedge(girl,boy,);
- addedge(s,girl,);
- addedge(boy,t,);
- end;
- readln(m);
- while m> do begin
- dec(m);
- readln(ss);
- i:=pos(' ',ss);
- j:=find(copy(ss,,i-));
- k:=find(copy(ss,i+,length(ss)-i));
- addedge(j,k,);
- end;
- end;
- function sap(x,flow:longint):longint;
- var
- now,more,i,too:longint;
- begin
- if x=t then exit(flow);
- now:=;
- i:=cur[x];
- while i>= do begin
- too:=edge[i].toward;
- if (d[x]=d[too]+) and (edge[i].cap>) then begin
- more:=sap(too,min(flow-now,edge[i].cap));
- dec(edge[i].cap,more);
- inc(edge[i xor ].cap,more);
- inc(now,more);
- cur[x]:=i;
- if flow=now then exit(flow);
- end;
- i:=edge[i].next;
- end;
- dec(gap[d[x]]);
- if gap[d[x]]= then d[s]:=tot;
- inc(d[x]);
- inc(gap[d[x]]);
- cur[x]:=first[x];
- exit(now);
- end;
- procedure maxflow;
- var
- i:longint;
- begin
- fillchar(gap,sizeof(gap),);
- fillchar(d,sizeof(d),);
- gap[]:=tot;
- for i:= to tot do cur[i]:=first[i];
- while d[s]<tot do sap(s,maxlongint);
- end;
- procedure tarjan(x:longint);
- var
- i,j,too:longint;
- begin
- inc(time);
- dfn[x]:=time;
- low[x]:=time;
- inc(top);
- p[top]:=x;
- chose[x]:=true;
- i:=first[x];
- while i>= do begin
- if edge[i].cap> then begin
- too:=edge[i].toward;
- if dfn[too]= then begin
- tarjan(too);
- low[x]:=min(low[x],low[too]);
- end
- else
- if chose[too] then
- low[x]:=min(low[x],low[too]);
- end;
- i:=edge[i].next;
- end;
- if low[x]=dfn[x] then begin
- inc(scc);
- repeat
- j:=p[top];
- dec(top);
- chose[j]:=false;
- belong[j]:=scc;
- until j=x;
- end;
- end;
- procedure work;
- var
- head,tail,i,j,k,l,x,too:longint;
- flag:boolean;
- begin
- time:=;
- fillchar(chose,sizeof(chose),false);
- fillchar(dfn,sizeof(dfn),);
- top:=;
- for i:= to tot do
- if dfn[i]= then tarjan(i);
- for i:= to n do begin
- j:=e[i];
- k:=edge[j].from;
- l:=edge[j].toward;
- if (belong[k]=belong[l]) or (edge[j].cap>) then writeln('Unsafe')
- else writeln('Safe');
- end;
- end;
- begin
- into;
- maxflow;
- work;
- end.
匈牙利版
- type
- arr=record
- toward,next,from:longint;
- flag:boolean;
- end;
- const
- maxn=;
- maxm=;
- var
- edge:array[..maxm]of arr;
- first,cur,d,p,gap,num,e,match,matche:array[..maxn]of longint;
- chose:array[..maxn]of boolean;
- trie:array[..maxn,'A'..'z'] of longint;
- n,esum,s,t,tot,total,peo,time,scc,top:longint;
- procedure addedge(j,k:longint);
- begin
- inc(esum);
- edge[esum].from:=j;
- edge[esum].toward:=k;
- edge[esum].next:=first[j];
- edge[esum].flag:=true;
- first[j]:=esum;
- end;
- function find(ss:string):longint;
- var
- i,u:longint;
- begin
- u:=;
- for i:= to length(ss) do u:=trie[u][ss[i]];
- exit(num[u]);
- end;
- procedure into;
- var
- i,j,k,m,u,boy,girl:longint;
- ss:string;
- begin
- esum:=;
- tot:=;
- total:=;
- peo:=;
- fillchar(first,sizeof(first),);
- readln(n);
- for i:= to n do begin
- readln(ss);
- ss:=ss+' ';
- u:=;
- j:=;
- while (ss[j]<>' ') do begin
- if trie[u][ss[j]]= then begin
- inc(total);
- trie[u][ss[j]]:=total;
- end;
- u:=trie[u][ss[j]];
- inc(j);
- end;
- inc(peo);
- num[u]:=peo;
- girl:=peo;
- inc(j);
- u:=;
- while (ss[j]<>' ') do begin
- if trie[u][ss[j]]= then begin
- inc(total);
- trie[u][ss[j]]:=total;
- end;
- u:=trie[u][ss[j]];
- inc(j);
- end;
- inc(peo);
- num[u]:=peo;
- boy:=peo;
- e[i]:=esum+;
- addedge(girl,boy);
- end;
- readln(m);
- while m> do begin
- dec(m);
- readln(ss);
- i:=pos(' ',ss);
- j:=find(copy(ss,,i-));
- k:=find(copy(ss,i+,length(ss)-i));
- addedge(j,k);
- end;
- end;
- function dfs(x:longint):boolean;
- var
- i,too:longint;
- begin
- i:=first[x];
- while i> do begin
- too:=edge[i].toward;
- if edge[i].flag and chose[too] then begin
- chose[too]:=false;
- if (match[too]=) or dfs(match[too]) then begin
- edge[matche[too]].flag:=true;
- matche[too]:=i;
- match[too]:=x;
- edge[i].flag:=false;
- exit(true);
- end;
- end;
- i:=edge[i].next;
- end;
- exit(false);
- end;
- procedure work;
- var
- i,j,boy,girl:longint;
- begin
- for i:= to n do begin
- fillchar(chose,sizeof(chose),true);
- dfs(edge[e[i]].from);
- end;
- //for i:= to n do writeln(match[i<<]);
- for i:= to n do begin
- fillchar(chose,sizeof(chose),true);
- j:=e[i];
- if edge[j].flag then begin
- writeln('Unsafe');
- continue;
- end;
- boy:=edge[j].toward;
- girl:=edge[j].from;
- match[boy]:=;
- if not dfs(girl) then begin
- match[boy]:=girl;
- matche[boy]:=j;
- writeln('Safe');
- end
- else writeln('Unsafe');
- end;
- end;
- begin
- into;
- work;
- end.
bzoj 2140: 稳定婚姻 (二分图)的更多相关文章
- BZOJ 2140 稳定婚姻 ——二分图
论二分图的可行边与必须边. 考虑用dinic增广之后的图,一些是必要的割边,一些是可行的割边. 我们首先求出一组可行的最大匹配,那么这些变都是可行的. 然后我们求一遍强连通分量. 如果 scc[u]! ...
- BZOJ 2140 稳定婚姻
2140: 稳定婚姻 Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...
- 2140: 稳定婚姻 - BZOJ
Description 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就 ...
- 【BZOJ】2140 稳定婚姻
[解析]Hash,离散化.Tarjan [分析] 对于每一个名字.首先离散化成编号. 用hash或者其它,反正不要最主要的即可了.否则O(N^2L)会爆掉. 然后请參考:http://www.cnbl ...
- BZOJ2140: 稳定婚姻
题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...
- BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)
2140: 稳定婚姻 Time Limit: 2 Sec Memory Limit: 259 MBSubmit: 1321 Solved: 652[Submit][Status][Discuss] ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- 洛谷 P1407 [国家集训队]稳定婚姻 解题报告
P1407 [国家集训队]稳定婚姻 题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的 ...
- 【bzoj2140】: 稳定婚姻 图论-tarjan
[bzoj2140]: 稳定婚姻 哎..都是模板题.. 一眼看过去 哇 二分图哎 然后发现好像并不能匈牙利算法 自己xjb画两张图,发现二分图左向右连配偶的边,然后右向左连交往过的边 然后如果Bi G ...
随机推荐
- 理解Python的装饰器
看Flask文档时候看到关于cache的装饰器,有这么一段代码: def cached(timeout=5 * 60, key=’view/%s’): def decorator(f): @wraps ...
- Visual Studio 起始页中不显示最近使用的项目的解决办法
将 HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/NoRecentDocsHistory的 ...
- zipaligin的使用介绍
近来一直在做APK反编译和重编译的工作,针对一些apk需要放入一些相应的文件,(当然这里不涉及非法盈利,都是有合约的),在对一些包打包以后,发现可以通过一个叫做zipalign的工具进行优化,对于这个 ...
- photoshop cc 2018安装破解教程(破解补丁,亲测,绝对可用)
破解步骤说明:下载地址百度网盘,https://pan.baidu.com/s/1cWtpUesl2fms3tFwEC0MiQ 1.右键解压Adobe Photoshop CC 2018 64位这个文 ...
- Ping隧道
1.研究原因: 校园内网的探索,校内电子图书馆资源的利用,认证校园网 2.目的: 内网服务器:在一台因防火墙等原因仅限icmp数据通过的 公网服务器 : 建立icmp 隧道链接, 并在公网服务器上进 ...
- 180605-Linux下Crontab实现定时任务
Linux下Crontab实现定时任务 基于Hexo搭建的个人博客,是一种静态博客页面,每次新增博文或者修改,都需要重新的编译并发布到Github,这样操作就有点蛋疼了,一个想法就自然而然的来了,能不 ...
- web自动化原理揭秘
做过两年自动化测试的小伙伴说web自动化测试真的不难,无非就是一些浏览器操作,页面元素操作,常规的情况很容易处理,再学一学特殊元素的处理,基本就能应付项目的测试了. 这个话倒没错,但是真正要学好自动化 ...
- Linux命令应用大词典-第28章 硬件管理
28.1 lscpu:显示有关CPU架构的信息 28.2 nproc:显示当前进程可用的CPU数目 28.3 chcpu:配置CPU
- 微信小程序入门学习之事件 事件对象 冒泡非冒泡事件(1)
这关于事件的学习,可以自己复制到微信开发者工具上自己运行试试. 首先这里有两个文件.js 和.wxml 文件 首先给出.js文件下代码 // pages/news/news.js Page({ /** ...
- 孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1
孤荷凌寒自学python第八十五天配置selenium并进行模拟浏览器操作1 (完整学习过程屏幕记录视频地址在文末) 要模拟进行浏览器操作,只用requests是不行的,因此今天了解到有专门的解决方案 ...