Description
Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlong's island. When he got there, he found the secret place where his friend was kept, but he could not go straight in. He saw a large door in front of him and two locks in the door. Beside the large door, he found a strange rock, on which there were some odd words. The sentences were encrypted. But that was easy for Ratish, an amateur cryptographer. After decrypting all the sentences, Ratish knew the following facts:
Behind the large door, there is a nesting prison, which consists of M floors. Each floor except the deepest one has a door leading to the next floor, and there are two locks in each of these doors. Ratish can pass through a door if he opens either of the two locks in it. There are 2N different types of locks in all. The same type of locks may appear in different doors, and a door may have two locks of the same type. There is only one key that can unlock one type of lock, so there are 2N keys for all the 2N types of locks. These 2N keys were divided into N pairs, and once one key in a pair is used, the other key will disappear and never show up again.
Later, Ratish found N pairs of keys under the rock and a piece of paper recording exactly what kinds of locks are in the M doors. But Ratish doesn't know which floor Luffy is held, so he has to open as many doors as possible. Can you help him to choose N keys to open the maximum number of doors?
Input
There are several test cases. Every test case starts with a line containing two positive integers N (1 <= N <= 210) and M (1 <= M <= 211) separated by a space, the first integer represents the number of types of keys and the second integer represents the number of doors. The 2N keys are numbered 0, 1, 2, ..., 2N - 1. Each of the following N lines contains two different integers, which are the numbers of two keys in a pair. After that, each of the following M lines contains two integers, which are the numbers of two keys corresponding to the two locks in a door. You should note that the doors are given in the same order that Ratish will meet. A test case with N = M = 0 ends the input, and should not be processed.
Output
For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.
Sample Input
3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0
Sample Output
4
Source
Beijing 2005
题目大意:
有2n把钥匙和m扇门。这2n把钥匙被分成若干对,每对只能选一把来使用。每扇门可以被指定的两把钥匙打开(只用其中一把)。只有按顺序打开前i-1扇门才能打开第i扇门。问最多可以打开多少扇门。
思路:
明显的2-SAT
设有一把钥匙x,x表示选,~x表示不选.
如果有一对钥匙a,b,则a->~b,b->~a,即a,b不同时存在.
如果有一个门的所需要的钥匙是a,b,则~a->b,~b->a,即a,b不同时不存在.
二分查找答案判断
源代码/pas:
type
edge=record
x,y,next:Longint;
end;
var
maxE,n,m,t:longint;
comp,low,dfn,ls,s:array[0..3024]of longint;
num,x1,x2,y1,y2:array[0..3024]of longint;
v:array[0..3024]of boolean;
e:array[0..100024]of edge;
function min(x,y:Longint):longint;
begin
min:=x;
if y<x then
min:=y;
end;
function nx(x:longint):longint;
begin
if x>n then
nx:=x-n
else
nx:=x+n;
end;
procedure add(x,y:Longint);
begin
inc(maxE);
e[maxE].x:=x;
e[maxE].y:=y;
e[maxE].next:=ls[x];
ls[x]:=maxE;
End;
procedure tarjan(x:longint);
var
i,y:longint;
begin
inc(t);
dfn[x]:=t;
low[x]:=t;
inc(s[0]);
s[s[0]]:=x;
v[x]:=true;
i:=ls[x];
while i>0 do
begin
y:=e[i].y;
if dfn[y]=0 then
begin
tarjan(y);
low[x]:=min(low[y],low[x]);
end
else
if (v[y]) then
low[x]:=min(low[x],dfn[y]);
i:=e[i].next;
end;
if dfn[x]=low[x] then
begin
inc(comp[0]);
repeat
y:=s[s[0]];
comp[y]:=comp[0];
dec(s[0]);
v[y]:=false;
until x=y;
end;
end;
function check:Boolean;
var
i:longint;
begin
t:=0;
check:=true;
fillchar(v,sizeof(v),false);
fillchar(dfn,sizeof(dfn),0);
fillchar(low,sizeof(low),0);
fillchar(comp,sizeof(comp),0);
fillchar(s,sizeof(s),0);
for i:=1 to n*2 do
if dfn[i]=0 then tarjan(i);
for i:=1 to n do
if comp[i]=comp[i+n] then
exit(false);
end;
procedure build(x:longint);
var
i:longint;
begin
maxE:=0;
fillchar(ls,sizeof(ls),0);
fillchar(e,sizeof(e),0);
for i:=1 to x do
begin
add(nx(y1[i]),y2[i]);
add(nx(y2[i]),y1[i]);
end;
end;
procedure init;
var
i,j:longint;
begin
for i:=1 to n do
begin
readln(x1[i],x2[i]);
inc(x1[i]);
inc(x2[i]);
num[x1[i]]:=i;
num[x2[i]]:=i+n;
end;
for i:=1 to m do
begin
readln(y1[i],y2[i]);
inc(y1[i]);
inc(y2[i]);
y1[i]:=num[y1[i]];
y2[i]:=num[y2[i]];
end;
end;
procedure main;
var
l,r,mid,ans:longint;
begin
readln(n,m);
if (n=0)and(m=0) then exit;
init;
ans:=0;
l:=0;
r:=m;
while l<=r do
begin
mid:=(l+r)div 2;
build(mid);
if check then
begin
ans:=ans+mid-min(ans,mid);
l:=mid+1;
end
else
r:=mid-1;
end;
writeln(ans);
end;
begin
while not eof do
main;
end.