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年半时间继续提高解决问题 ...
随机推荐
- OpenJudge 2980 大整数乘法
链接地址:http://bailian.openjudge.cn/practice/2980/ 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个不超过200位的非负整数的积 ...
- slabs.c
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Slabs memory allo ...
- centos 安装php ide (eclipse + php 插件)
1.检查更新并安装eclipse yum check-update yum install eclipse*此时的 eclipse 已经安装好了,默认是在/usr/lib/下的,可以通过cd /u ...
- Application.StartupPath同System.Environment.CurrentDirectory区别
System.Windows.Forms.Application.StartupPath:获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. System.Environment.Curr ...
- 换行符‘\n’和回车符‘\r’
顾名思义,换行符就是另起一行,回车符就是回到一行的开头,所以我们平时编写文件的回车符应该确切来说叫做回车换行符 '\n' 10 换行(newline)'\r' 13 回车(return) 也可以表示为 ...
- 解决Twitter Bootstrap Tab URL链接问题
例如这样的一个Tabs 代码: <ul class="nav nav-tabs" id="myTab"> <li class="ac ...
- 一些web编程能用到的小知识
1 信用卡验证算法-luhn算法.(in django/utils/checksums.py) 1.从卡号最后一位数字开始,逆向将奇数位(1.3.5等等)相加.2.将偶数位数字相加,但是这里有个麻烦. ...
- NodeJS+Express+Socket.io的一个简单例子
关键字:NodeJS,Express,Socket.io. OS:Windows 8.1 with update pro. 1.安装NodeJS:http://nodejs.org/. 2.初始化一个 ...
- 创建第一个UI
创建一个2D UI 制作UI时,首先要创建UI的"根".在Unity顶部NGUI菜单中选择Create,然后选择2D UI. 创建完成后,在Scene窗口中,NGUI自动生成了一个 ...
- EL四大作用域 9个jsp对象有效范围 及 对应的类
java中request,session,application的作用范围 page,request,session,application四者的作用范围: page的作用范围是当前页面:对应El表达 ...