poj1144 割点模板

tarjan求割点(low[p]>=dfn[x]) 的模板题,注意对dfs序列根节点情况的单独考虑(必须访问至少两次)

var
        n,ans,l,x,y     :longint;
        time,son        :longint;
        last,low,dfn    :array[0..110] of longint;
        vis,flag        :array[0..110] of boolean;
        pre,other       :array[0..20010] of longint;
        i               :longint;
procedure connect(x,y:longint);
begin
   inc(l);
   pre[l]:=last[x];
   last[x]:=l;
   other[l]:=y;
end;

function min(a,b:longint):longint;
begin
   if a<b then exit(a) else exit(b);
end;

procedure dfs(x:longint);
var
        p,q:longint;
begin
   inc(time);
   low[x]:=time;
   dfn[x]:=time;
   vis[x]:=true;
   //
   q:=last[x];
   while (q<>0) do
   begin
      p:=other[q];
      if dfn[p]=0 then
      begin
         dfs(p);
         low[x]:=min(low[x],low[p]);
         if (low[p]>=dfn[x]) and (x<>1) then flag[x]:=true
          else if (x=1) and (low[p]>=dfn[x]) then inc(son);
      end else
      if vis[p] then low[x]:=min(low[x],dfn[p]);
      q:=pre[q];
   end;
end;

begin
   read(n);
   while (n<>0) do
   begin
      l:=0;ans:=0;son:=0;time:=0;
      fillchar(last,sizeof(last),0);
      fillchar(dfn,sizeof(dfn),0);
      fillchar(low,sizeof(low),0);
      fillchar(vis,sizeof(vis),false);
      fillchar(flag,sizeof(flag),false);
      read(x);
      while (x<>0) do
      begin
         while not eoln do
         begin
            read(y);
            connect(x,y);
            connect(y,x);
         end;
         read(x);
      end;
      for i:=1 to n do if dfn[i]=0 then dfs(i);
      if (son>1) then inc(ans);
      for i:=1 to n do if flag[i] then inc(ans);
      writeln(ans);
      read(n);
   end;
end.


——by Eirlys

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值