poj2723 Get Luffy Out 2011-12-26
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 __________________________________
1 Program Stone; 2 var n,m,deep,le,lt,ans:longint; 3 flag:boolean; 4 head,t,pair,dfn,low,stack,tr:array[0..20000]of longint; 5 next,date:array[1..400000]of longint; 6 a,b:array[1..4000,1..2]of longint; 7 procedure add(x,y:longint); 8 begin 9 inc(le); 10 date[le]:=y; 11 next[le]:=head[x]; 12 head[x]:=le; 13 end; 14 function min(a,b:longint):longint; 15 begin 16 if a<b then min:=a else min:=b; 17 end; 18 procedure tarjan(x:longint); 19 var i:longint; 20 begin 21 inc(deep); 22 dfn[x]:=deep; 23 low[x]:=deep; 24 stack[deep]:=x; 25 i:=head[x]; 26 while i<>0 do 27 begin 28 if dfn[date[i]]=0 then begin 29 tarjan(date[i]); 30 low[x]:=min(low[x],low[date[i]]); 31 end 32 else if tr[date[i]]=0 then low[x]:=min(low[x],dfn[date[i]]); 33 i:=next[i]; 34 end; 35 if dfn[x]=low[x] then 36 begin 37 inc(lt); 38 repeat 39 tr[stack[deep]]:=lt; 40 dec(deep); 41 until stack[deep+1]=x; 42 end; 43 end; 44 Procedure insert(x:longint); //重新建图 45 var i:longint; 46 begin 47 fillchar(head,sizeof(head),0); 48 fillchar(next,sizeof(next),0); 49 le:=0; 50 for i:=1 to n do 51 begin 52 add(a[i,1],a[i,2]+2*n); 53 add(a[i,2],a[i,1]+2*n); 54 end; 55 for i:=1 to x do 56 begin 57 add(b[i,1]+2*n,b[i,2]); 58 add(b[i,2]+2*n,b[i,1]); 59 end; 60 lt:=0; 61 fillchar(tr,sizeof(tr),0); 62 fillchar(dfn,sizeof(dfn),0); 63 for i:=0 to 4*n-1 do 64 if dfn[i]=0 then tarjan(i); 65 flag:=true; 66 for i:=0 to 2*n-1 do 67 if (tr[i]=tr[i+2*n]) then begin flag:=false;break;end; 68 end; 69 procedure init; 70 var i,j,k,lc,rc,mc:longint; 71 begin 72 for i:=1 to n do readln(a[i,1],a[i,2]); 73 for i:=1 to m do 74 readln(b[i,1],b[i,2]); 75 lc:=0;rc:=m+1; 76 while (lc+1<rc) do //二分 77 begin 78 mc:=(lc+rc)div 2; 79 insert(mc); 80 if flag then lc:=mc 81 else rc:=mc; 82 end; 83 writeln(lc); 84 end; 85 Begin 86 assign(input,'input.in');reset(input); 87 readln(n,m); 88 while (n<>0) do 89 begin 90 init; 91 readln(n,m); 92 end; 93 end. 94 95