Description
Ratish isa young man who always dreams of being a hero. One day his friend Luffy wascaught by Pirate Arlong. Ratish set off at once to Arlong's island. When he gotthere, he found the secret place where his friend was kept, but he could not gostraight 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 oddwords. The sentences were encrypted. But that was easy for Ratish, an amateurcryptographer. After decrypting all the sentences, Ratish knew the followingfacts:
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, andthere are two locks in each of these doors. Ratish can pass through a door ifhe opens either of the two locks in it. There are 2N different types of locksin all. The same type of locks may appear in different doors, and a door mayhave two locks of the same type. There is only one key that can unlock one typeof lock, so there are 2N keys for all the 2N types of locks. These 2N keys weredivided into N pairs, and once one key in a pair is used, the other key willdisappear and never show up again.
Later, Ratish found N pairs of keys under the rock and a piece of paperrecording exactly what kinds of locks are in the M doors. But Ratish doesn'tknow 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 areseveral test cases. Every test case starts with a line containing two positiveintegers N (1 <= N <= 210) and M (1 <= M <= 211)separated by a space, the first integer represents the number of types of keysand the second integer represents the number of doors. The 2N keys are numbered0, 1, 2, ..., 2N - 1. Each of the following N lines contains two differentintegers, which are the numbers of two keys in a pair. After that, each of thefollowing M lines contains two integers, which are the numbers of two keyscorresponding to the two locks in a door. You should note that the doors aregiven in the same order that Ratish will meet. A test case with N = M = 0 endsthe input, and should not be processed.
Output
For eachtest case, output one line containing an integer, which is the maximum numberof 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
大意:
有2*N把不同的锁,每把锁有一个钥匙,所以共有2*N 把钥匙。把2*N把钥匙两两配对共分为N组。
有个M层楼,每层楼有一个门,每个门上有两把锁,可能是相同的也可能是不同的。 走上某层楼之前,必须要打开这个门上的至少一个锁。
要你从每组钥匙中选择一把钥匙,然后用这些钥匙去上这栋楼,问最多能走到几层楼?
分析:
题目可能有多组数据。对于一个门,且门上的锁不是同一组,假设第一个为(x1,y1),另一个就是(x2,y2),不能同时选x2,y2,因为这一个门就开不了了。所以连边x1-->y2,y2—>x1.
var
n,m,l,r,ans,mid,i,cnt,sum,d,e:longint;
flag:boolean;
x,y,p,q,stack,low,dfn,belong,ls,a:array[1..30000] of longint;
f:array[1..30000] of boolean;
g:array[1..1000000]of record
x,y,next:longint;
end;
procedure add(x,y:longint);
begin
inc(e);
g[e].x:=x;
g[e].y:=y;
g[e].next:=ls[x];
ls[x]:=e;
end;
function min(x,y:longint):longint;
begin
ifx<y then exit(x)
else exit(y);
end;
procedure dfs(x:longint);
var
i:longint;
begin
inc(d);
low[x]:=d;
dfn[x]:=d;
inc(cnt);
stack[cnt]:=x;
f[x]:=true;
i:=ls[x];
while i>0 do
with g[i] do
begin
if dfn[y]=0
then begin
dfs(y);
low[x]:=min(low[x],low[y]);
end
else if f[y] then low[x]:=min(low[x],dfn[y]);
i:=next;
end;
ifdfn[x]=low[x] then
begin
inc(sum);
repeat
i:=stack[cnt];
f[i]:=false;
dec(cnt);
belong[i]:=sum;
until i=x;
end;
end;
function next(x:longint):longint;
begin
ifx>n then exit(x-n)
else exit(x+n);
end;
begin
readln(n,m);
while n+m>0 do
begin
for i:=1 to n do
begin
readln(x[i],y[i]);
inc(x[i]);
inc(y[i]);
a[x[i]]:=i;
a[y[i]]:=i+n;
end;
for i:=1 to m do
begin
readln(p[i],q[i]);
inc(p[i]);
inc(q[i]);
p[i]:=a[p[i]];
q[i]:=a[q[i]];
end;
l:=0;
r:=m;
ans:=0;
while l<=r do
begin
mid:=(l+r) div 2;
e:=0;
fillchar(ls,sizeof(ls),0);
for i:=1 to mid do
begin
add(next(p[i]),q[i]);
add(next(q[i]),p[i]);
end;
fillchar(low,sizeof(low),0);
fillchar(dfn,sizeof(dfn),0);
fillchar(f,sizeof(f),false);
d:=0;
cnt:=0;
sum:=0;
for i:=1 to n*2 do
if dfn[i]=0 then dfs(i);
flag:=true;
for i:=1 to n do
if belong[i]=belong[i+n] then
begin
flag:=false;
break;
end;
if flag
then begin
if mid>ans then ans:=mid;
l:=mid+1;
end
else r:=mid-1;
end;
writeln(ans);
readln(n,m);
end;
end.