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年半时间继续提高解决问题 ...
随机推荐
- QT宏 Q_OBJECT,explicit, QHostAddress, quint, emit
QT相關 一. 參考: 1.宏Q_OBJECT 二. explicit struct constrcution 三. QHostAddress Detailed Description: The QH ...
- 什么是SQL statement?
什么是SQL statement? 1.SQL SELECT statement - SELECT命令 REFER: What is SQL, and what are some example st ...
- linux下screen工具的简单使用
有时候,希望即使退出终端了,下次登录linux的时候,还能回到程序的控制界面,这个时候,screen工具就很有用了例如,写了一个从控制台读取屏幕输入的程序input_test,如果从终端退出了,下次登 ...
- MVC——分页控件
不管是什么类型的网站,分页都是必不可少的功能实现.在这里记录一下我自己接触过的分页控件: 一. MvcPager控件(记得项目里添加MvcPager.dll的引用) 这里面比较常用的就 ——@Html ...
- C# winform关于DataGridView的一些操作
设置字段名 设置字段值 设定单元格表示 Error图标 设定当前单元格 取得当前单元格内容 取得当前单元格的列 Index 取得当前单元格的行 Index 向下一行 向上一行 取消 DataGridV ...
- C#调用金数据API
首先,吐槽一下金数据的API文档 http://help.jinshuju.net/articles/api-intro.html写的很粗糙啊...反正我是没太看明白 拿表单api举例,只告诉你了个地 ...
- AVAudioPlayer 播放音频
play方法 实现立即播放音频功能 pause方法 可以对播放暂停 stop方法 可以停止播放行为 注: pause & stop的不同之处: 调用stop方法会撤销调用prepareToPl ...
- Redhat 6 配置CentOS yum source
由于最近曝出linux的bash漏洞,想更新下bash,于是 想到了配置CentOS yum source. 测试bash漏洞的命令: env x='() { :;}; echo "Your ...
- about building flying sauser
download flying sauser: unzip flyingsaucer-master.zip cd flyingsaucer-master/ mvn install
- 初探—KMP模式匹配算法
KMP算法思想: 普通的字符串匹配算法S主串必须要回溯.但回溯就影响了效率. 改进的地方也就是这里,我们从P 串本身出发,事先就找准了T自身前后部分匹配的位置,那就可以改进算法. next数组的含义: ...