poj2723
把每对钥匙看做一个变量,那两个钥匙看做他的两个状态
每一个开门的要求就是一个条件(xi or xj)
很显然有了2sat的基本要素
2sat是一个判定性问题,而这题求最多能过几个门;
不难想到二分答案,转化为判定性问题可轻松解决
type node=record
next,point:longint;
end;
var edge:array[..] of node;
v,f:array[..] of boolean;
other,d1,d2,be,dfn,low,p,st:array[..] of longint;
mid,l,r,n,m,x,y,ans,len,sum,t,h,i:longint; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure add(x,y:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].next:=p[x];
p[x]:=len;
end; procedure tarjan(x:longint);
var i,y:longint;
begin
inc(h);
inc(t);
st[t]:=x;
dfn[x]:=h;
low[x]:=h;
f[x]:=true;
v[x]:=true;
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if not v[y] then
begin
tarjan(y);
low[x]:=min(low[x],low[y]);
end
else if f[y] then low[x]:=min(low[x],low[y]);
i:=edge[i].next;
end;
if low[x]=dfn[x] then
begin
inc(sum);
while st[t+]<>x do
begin
y:=st[t];
f[y]:=false;
be[y]:=sum;
dec(t);
end;
end;
end; function check(k:longint):boolean;
var i,x,y:longint;
begin
len:=;
fillchar(p,sizeof(p),);
fillchar(v,sizeof(v),false);
fillchar(f,sizeof(f),false);
fillchar(st,sizeof(st),);
fillchar(be,sizeof(be),);
sum:=;
for i:= to k do
begin
x:=d1[i];
y:=d2[i];
add(other[x],y);
add(other[y],x);
end;
for i:= to *n- do
if not v[i] then
begin
t:=;
h:=;
tarjan(i);
end;
、
for i:= to *n- do
if be[other[i]]=be[i] then exit(false);
exit(true);
end; begin
readln(n,m);
while (n<>) and (m<>) do
begin
len:=;
fillchar(p,sizeof(p),);
for i:= to n do
begin
readln(x,y);
other[x]:=y;
other[y]:=x;
end;
for i:= to m do
readln(d1[i],d2[i]);
ans:=;
l:=;
r:=m;
while l<=r do
begin
mid:=(l+r) shr ;
if check(mid) then
begin
ans:=mid;
l:=mid+;
end
else r:=mid-;
end;
writeln(ans);
readln(n,m);
end;
end.
poj2723的更多相关文章
- hdu3715 Go Deeper[二分+2-SAT]/poj2723 Get Luffy Out[二分+2-SAT]
这题转化一下题意就是给一堆形如$a_i + a_j \ne c\quad (a_i\in [0,1],c\in [0,2])$的限制,问从开头开始最多到哪条限制全是有解的. 那么,首先有可二分性,所以 ...
- 【POJ2723】Get Luffy Out - 二分+2-SAT
题面描述 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by ...
- Poj2723:Get Luffy Out
题意 给出 n 对钥匙,每对只能挑一把使用,每把只能用一次,当一对钥匙中的一把被使用后,另一把也就不能再用了:然后给出 m 道门,每个门都有两把钥匙可以打开,问最多能开几道门(按给出的顺序开). So ...
- poj2723 2-sat
当两个门锁相同时,这个钥匙必须用,不同时分开用 可以直接遍历门,当然二分更快 #include<map> #include<set> #include<cmath> ...
- POJ2723 Get Luffy Out 【2-sat】
题目 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by P ...
- poj2723 2sat判断解+二分
典型的2-sat问题,题意:有m个门,每个门上俩把锁,开启其中一把即可,现在给n对钥匙(所有 钥匙编号0123456...2n-1),每对钥匙只能用一把,要求尽可能开门多(按顺序,前max个). 关键 ...
- POJ2723 Get Luffy Out解题报告tarjan+2-SAT+二分
今天看到讲2-SAT比较好的blog,感觉微微的理解了2-SAT 传送门 参考: https://blog.csdn.net/leolin_/article/details/6680144 题意:你有 ...
- POJ2723 题解
WA了半天才发现居然是因为没看见这道题有多组数据,wzfl... 题目大意:有N对钥匙,对于每一对钥匙,如果使用了其中一把,另一把钥匙就会消失.接下来有M扇门,每扇门上有两把锁,分别对应两把钥匙(锁会 ...
- ACM训练计划step 2 [非原创]
(Step2-500题)POJ训练计划+SGU 经过Step1-500题训练,接下来可以开始Step2-500题,包括POJ训练计划的298题和SGU前两章200题.需要1-1年半时间继续提高解决问题 ...
随机推荐
- 理解css 中的position五个属性
在实际开发页面布局时,运用position,对定位的块级元素的嵌套的效果总是不太理解,这里做了几个测试 一般的在w3c中我们可以很容易的获取定义: static : 默认值.没有定位,元素出现在正常的 ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- [Guava官方文档翻译] 6. 用Guava辅助Throwable异常处理 (Throwables Explained)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3537508.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...
- linux 和 windows下的程序计时
Windows 使用<windows.h>中的GetTickCount(),该函数获得从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD. 转自:http://w ...
- 修改Sublime Text 3 的侧边栏字体大小
此文转载哈,忘了作者地址,还请见谅!! 首先需要确保安装了Package ControlPackage Control作为ST必备插件,这里就不多介绍了,没装的话,google一下,各种介绍以及安装教 ...
- js组件开发流程
html代码 <div id="div1"></div> <div id="div2"></div> <d ...
- 使用ajaxFileUpload实现异步上传图片
index.html <head runat="server"> <title></title> <script src="jq ...
- jexus 启动失败 原因定位
现象: root@test:/usr/jexus/siteconf# /usr/jexus/jws restartRestarting ... Failure 定位步骤: 1.查看/usr/jexus ...
- 开发软件设计模型 visual studio UML
http://www.ibm.com/developerworks/cn/rational/rationaledge/content/feb05/bell/ http://msdn.microsoft ...
- 纯JavaScript实现一些小功能
题目链接:http://wenku.baidu.com/link?url=7Gbarr5q9X6h1QFRVAsHmfPp1xXagG209mvrJqBogseb4WLeRqbVKwxQieoh8SL ...