您的位置:首页 > 其它

poj2723 Get Luffy Out 2011-12-26

2016-03-02 20:19 295 查看
Get Luffy OutTime Limit: 2000MSMemory Limit: 65536KTotal Submissions: 5503Accepted: 2044

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 60 31 24 50 10 24 14 23 52 20 0

Sample Output

4 __________________________________

Program Stone;
var n,m,deep,le,lt,ans:longint;
flag:boolean;
head,t,pair,dfn,low,stack,tr:array[0..20000]of longint;
next,date:array[1..400000]of longint;
a,b:array[1..4000,1..2]of longint;
procedure add(x,y:longint);
begin
inc(le);
date[le]:=y;
next[le]:=head[x];
head[x]:=le;
end;
function min(a,b:longint):longint;
begin
if a<b then min:=a else min:=b;
end;
procedure tarjan(x:longint);
var i:longint;
begin
inc(deep);
dfn[x]:=deep;
low[x]:=deep;
stack[deep]:=x;
i:=head[x];
while i<>0 do
begin
if dfn[date[i]]=0 then begin
tarjan(date[i]);
low[x]:=min(low[x],low[date[i]]);
end
else if tr[date[i]]=0 then low[x]:=min(low[x],dfn[date[i]]);
i:=next[i];
end;
if dfn[x]=low[x] then
begin
inc(lt);
repeat
tr[stack[deep]]:=lt;
dec(deep);
until stack[deep+1]=x;
end;
end;
Procedure insert(x:longint); //重新建图
var i:longint;
begin
fillchar(head,sizeof(head),0);
fillchar(next,sizeof(next),0);
le:=0;
for i:=1 to n do
begin
add(a[i,1],a[i,2]+2*n);
add(a[i,2],a[i,1]+2*n);
end;
for i:=1 to x do
begin
add(b[i,1]+2*n,b[i,2]);
add(b[i,2]+2*n,b[i,1]);
end;
lt:=0;
fillchar(tr,sizeof(tr),0);
fillchar(dfn,sizeof(dfn),0);
for i:=0 to 4*n-1 do
if dfn[i]=0 then tarjan(i);
flag:=true;
for i:=0 to 2*n-1 do
if (tr[i]=tr[i+2*n]) then begin flag:=false;break;end;
end;
procedure init;
var i,j,k,lc,rc,mc:longint;
begin
for i:=1 to n do  readln(a[i,1],a[i,2]);
for i:=1 to m do
readln(b[i,1],b[i,2]);
lc:=0;rc:=m+1;
while (lc+1<rc) do //二分
begin
mc:=(lc+rc)div 2;
insert(mc);
if flag then lc:=mc
else rc:=mc;
end;
writeln(lc);
end;
Begin
assign(input,'input.in');reset(input);
readln(n,m);
while (n<>0) do
begin
init;
readln(n,m);
end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: