Get Luffy Out poj 2723 Tarjan+2-SAT

题意/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.

 

题解/solution

  首先二分答案,然后分析到每扇门就相当于一个or运算,然后建图:

  i表示选第i对钥匙的前一把,i+n表示选第i对钥匙的后一把,next(i)表示和钥匙i对应的另一把钥匙。

  假设某扇门需要钥匙i和j则连边next(i)->j  next(j)->i

  然后跑强连通分量。

  若有满足的i使得i和next(i)在同一强连通分量,则不满足条件,反之则满足。

代码/Code

const
  maxV=3001;
  maxE=300001;

type
  arr=record
    x,y,next:longint;
  end;

var
  n,m,ans,mid,t,sum,d,nm:longint;
  flag:boolean;
  tu:array[0..maxE] of arr;
  v:array[0..maxV] of boolean;
  x1,y1,x2,y2,stack,low,dfn,belong,ls,num:array[0..maxV] of longint;

procedure add(o,p:longint);
begin
  inc(nm);
  with tu[nm] do
    begin
      x:=o; y:=p;
      next:=ls[o];
      ls[o]:=nm;
    end;
end;

function min(t,k:longint):longint;
begin
  if t<k then exit(t);
  exit(k);
end;

procedure tarjan(o:longint);
var
  i:longint;
begin
  inc(d);
  low[o]:=d; dfn[o]:=d;
  inc(t);
  stack[t]:=o; v[o]:=true;
  i:=ls[o];
  while i>0 do
    with tu[i] do
      begin
        if dfn[y]=0 then
          begin
            tarjan(y);
            low[x]:=min(low[x],low[y]);
          end else
          if v[y] then low[x]:=min(low[x],dfn[y]);
        i:=next;
      end;
  if dfn[o]=low[o] then
    begin
      inc(sum);
      repeat
        i:=stack[t];
        v[i]:=false;
        dec(t);
        belong[i]:=sum;
      until i=o;
    end;
end;

procedure init;
var
  i:longint;
begin
  for i:=1 to n do
    begin
      readln(x1[i],y1[i]);
      inc(x1[i]); inc(y1[i]);
      num[x1[i]]:=i;
      num[y1[i]]:=i+n;
    end;
  for i:=1 to m do
    begin
      readln(x2[i],y2[i]);
      inc(x2[i]); inc(y2[i]);
      x2[i]:=num[x2[i]];
      y2[i]:=num[y2[i]];
    end;
end;

function next(x:longint):longint;
begin
  if x>n then exit(x-n)
         else exit(x+n);
end;

procedure main;
var
  i,l,r:longint;
begin
  l:=0; r:=m; ans:=0;
  while l<=r do
    begin
      mid:=(l+r) div 2;
      nm:=0;
      fillchar(ls,sizeof(ls),0);
      for i:=1 to mid do
        begin
          add(next(x2[i]),y2[i]);
          add(next(y2[i]),x2[i]);
        end;
      fillchar(low,sizeof(low),0);
      fillchar(dfn,sizeof(dfn),0);
      fillchar(v,sizeof(v),false);
      d:=0; t:=0; sum:=0;
      for i:=1 to n*2 do
        if dfn[i]=0 then tarjan(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;
end;

begin
  readln(n,m);
  while n+m>0 do
    begin
      init;
      main;
      writeln(ans);
      readln(n,m);
    end;
end.



原文地址:https://www.cnblogs.com/zyx-crying/p/9319682.html