Get Luffy Out poj 2723 Tarjan+2-SAT

面对复杂的密室谜题,主人公Ratish必须运用智慧选择合适的钥匙组以打开尽可能多的门,救出朋友Luffy。本篇介绍了一种通过构建特殊图结构并利用强连通分量算法来解决这一问题的方法。

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


Matlab基于粒子群优化算法及鲁棒MPPT控制器提高光伏并网的效率内容概要:本文围绕Matlab在电力系统优化与控制领域的应用展开,重点介绍了基于粒子群优化算法(PSO)和鲁棒MPPT控制器提升光伏并网效率的技术方案。通过Matlab代码实现,结合智能优化算法与先进控制策略,对光伏发电系统的最大功率点跟踪进行优化,有效提高了系统在不同光照条件下的能量转换效率和并网稳定性。同时,文档还涵盖了多种电力系统应用场景,如微电网调度、储能配置、鲁棒控制等,展示了Matlab在科研复现与工程仿真中的强大能力。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事新能源系统开发的工程师;尤其适合关注光伏并网技术、智能优化算法应用与MPPT控制策略研究的专业人士。; 使用场景及目标:①利用粒子群算法优化光伏系统MPPT控制器参数,提升动态响应速度与稳态精度;②研究鲁棒控制策略在光伏并网系统中的抗干扰能力;③复现已发表的高水平论文(如EI、SCI)中的仿真案例,支撑科研项目与学术写作。; 阅读建议:建议结合文中提供的Matlab代码与Simulink模型进行实践操作,重点关注算法实现细节与系统参数设置,同时参考链接中的完整资源下载以获取更多复现实例,加深对优化算法与控制系统设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值