Stop the Hollyweb! No DRM in HTML5.

[转]Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

部分转自http://hi.baidu.com/lydrainbowcat/item/f8a5ac223e092b52c28d591c

作者提示:在阅读本文之前,请确保您已经理解并掌握了基本的Tarjan算法,不会的请到http://hi.baidu.com/lydrainbowcat/blog/item/42a6862489c98820c89559f3.html阅读。

附:C++程序(我个人的)

void add(int x,int y)//加边
{
 ver[++tot]=y,next[tot]=head[x],head[x]=tot;
}

void tarjan(int x)
{
 dfn[x]=low[x]=++num;
 s[++p]=x,v[x]=1;
 for(int i=head[x];i;i=next[i])
  if(!dfn[ver[i]])
  {
   tarjan(ver[i]);
   low[x]=min(low[x],low[ver[i]]);
  }
  else if(v[ver[i]])
   low[x]=min(low[x],dfn[ver[i]]);
 if(low[x]==dfn[x])
 {
  int y; ++t;
  do{y=s[p--],v[y]=0; c[y]=t;}while(y!=x);//c数组标记每个点属于哪一个SCC
 }
}

 

program p1111;
//Tyvj P1111舞会
//Tarjan + object栈写法

uses math;

type
 Tstack=object
   t:array[0..1000] of longint;
   ins:array[0..1000] of boolean;
   Stop:longint;
   function isin(P:longint):boolean;
   function top:longint;
   function pop:longint;
   procedure push(P:longint);
 end;

 rec=record
   e,next:longint;
 end;

Var
 s:tstack;
 a:array[0..10000] of rec;
 b,dfn,low:array[0..1000] of longint;
 no,ans,n,i,ed,top:longint;

procedure add(st,ed:longint);inline;
  begin
  inc(top);
  with a[top] do
    begin
    e:=ed;
    next:=b[st];
  end;
  b[st]:=top;
end;

function tstack.top:longint;
  begin
  exit(t[stop]);
end;

function tstack.pop:longint;
  begin
  ins[t[stop]]:=false;
  pop:=t[stop];
  dec(stop);
end;

procedure tstack.push(P:longint);
  begin
  inc(stop);
  t[stop]:=p;
  ins[p]:=true;
end;

function tstack.isin(P:longint):boolean;
  begin
  exit(ins[p]);
end;

procedure tarjan(P:longint);
var
 u:longint;
 y:rec;
  begin
  inc(no);
  dfn[p]:=no;low[p]:=no; s.push(p);
  u:=b[p];
  while u<>0 do
    begin
    y:=a[u];
    u:=y.next;
    if dfn[y.e]=0 then
      begin
      tarjan(y.e);
      low[p]:=min(low[p],low[y.e]);
    end else
      if s.isin(y.e) then
      low[p]:=min(low[p],dfn[y.e]);
  end;
  if low[p]=dfn[p] then
    begin
    inc(ans);
    while s.top<>p do s.pop;
    s.pop;
  end;
end;

  begin
  readln(n); top:=0;
  for i:=1 to n do
    begin
    ed:=1;
    while ed<>0 do
      begin
      read(ed);
      if ed=0 then break;
      add(i,ed);
    end;
    readln;
  end;
  ans:=0;
  no:=0;
  for i:=1 to n do
    if dfn[i]=0 then
      tarjan(i);
  writeln(ans);
  readln;
end.

 

 

基本概念:

1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点

2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合

3.点连通度:最小割点集合中的顶点数。

4.割边(桥):删掉它之后,图必然会分裂为两个或两个以上的子图。

5.割边集合:如果有一个边集合,删除这个边集合以后,原图变成多个连通块,就称这个点集为割边集合

6.边连通度:一个图的边连通度的定义为,最小割边集合中的边数。

7.缩点:把没有割边的连通子图缩为一个点,此时满足任意两点之间都有两条路径可达。

注:求块<>求缩点。缩点后变成一棵k个点k-1条割边连接成的树。而割点可以存在于多个块中。

8.双连通分量:分为点双连通和边双连通。它的标准定义为:点连通度大于1的图称为点双连通图,边连通度大于1的图称为边双连通图。通俗地讲,满足任意两点之间,能通过两条或两条以上没有任何重复边的路到达的图称为双连通图。无向图G的极大双连通子图称为双连通分量

Tarjan算法的应用论述:

1.求强连通分量(见上一篇文章,本文第一行有链接)、割点、桥、缩点:

对于Tarjan算法中,我们得到了dfn和low两个数组,

low[u]:=min(low[u],dfn[v])——(u,v)为后向边,v不是u的子树;

low[u]:=min(low[u],low[v])——(u,v)为树枝边,v为u的子树;

下边对其进行讨论:

若low[v]>=dfn[u],则u为割点,节点v的子孙和节点u形成一个块。因为这说明v的子孙不能够通过其他边到达u的祖先,这样去掉u之后,图必然分裂为两个子图。这样我们处理点u时,首先递归u的子节点v,然后从v回溯至u后,如果发现上述不等式成立,则找到了一个割点u,并且u和v的子树构成一个块。

void tarjan(int x)
{
 v[x]=1,dfn[x]=low[x]=++num;
 for(int i=head[x];i;i=next[i])
  if(!v[ver[i]])
  {
   tarjan(ver[i]);
   low[x]=min(low[x],low[ver[i]]);
   if(dfn[x]<=low[ver[i]]) v[x]++;
  }
  else low[x]=min(low[x],dfn[ver[i]]);
 if((x==1&&v[x]>2)||(x>1&&v[x]>1)) v[x]=2; else v[x]=1;//v[x]=2表示该点为割点,注意其中第一个点要特判
}

 

若low[v]>dfn[u],则(u,v)为割边。 但是实际处理时我们并不这样判断,因为有的图上可能有重边,这样不好处理。我们记录每条边的标号(一条无向边拆成的两条有向边标号相同),记录每个点的父 亲到它的边的标号,如果边(u,v)是v的父亲边,就不能用dfn[u]更新low[v]。这样如果遍历完v的所有子节点后,发现 low[v]=dfn[v],说明u的父亲边(u,v)为割边。

void tarjan(int x)
{
 vis[x]=1;
 dfn[x]=low[x]=++num;
 for(int i=head[x];i;i=next[i])
  if(!vis[ver[i]])
  {
   p[ver[i]]=edge[i];//记录父亲边
   tarjan(ver[i]);
   low[x]=min(low[x],low[ver[i]]);
  }
  else if(p[x]!=edge[i])//不是父亲边才更新
   low[x]=min(low[x],dfn[ver[i]]);
 if(p[x]&&low[x]==dfn[x]) f[p[x]]=1;//是割边
}

 

 2.求双连通分量以及构造双连通分量:

对于点双连通分支,实际上在求割点的 过程中就能顺便把每个点双连通分支求出。建立一个栈,存储当前双连通分支,在搜索图时,每找到一条树枝边或后向边(非横叉边),就把这条边加入栈中。如果 遇到某时满足DFS(u)<=Low(v),说明u是一个割点,同时把边从栈顶一个个取出,直到遇到了边(u,v),取出的这些边与其关联的点,组 成一个点双连通分支。割点可以属于多个点双连通分支,其余点和每条边只属于且属于一个点双连通分支。

对于边双连通分支,求法更为简单。只需在求出所有的桥以后,把桥边删除,原图变成了多个连通块,则每个连通块就是一个边双连通分支。桥不属于任何一个边双连通分支,其余的边和每个顶点都属于且只属于一个边双连通分支。

一个有桥的连通图,如何把它通过加边变成边双连通图?方法为首先求出所有的桥,然后删除这些桥边,剩下的每个连通块都是一个双连通子图。把每个双连通子图收缩为一个顶点,再把桥边加回来,最后的这个图一定是一棵树,边连通度为1。

统 计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就 是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为 一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

3.求最近公共祖先(LCA)

在 遍历到u时,先tarjan遍历完u的子树,则u和u的子树中的节点的最近公共祖先就是u,并且u和【u的兄弟节点及其子树】的最近公共祖先就是u的父 亲。注意到由于我们是按照DFS顺序遍历的,我们可用一个color数组标记,正在访问的染色为1,未访问的标记为0,已经访问到即在【u的子树中的】及 【u的已访问的兄弟节点及其子树中的】染色标记为2,这样我们可以通过并查集的不断合并更新,通过find实现以上目标。

注:用链表存储边和问题,可以使得该算法的时间复杂度降低为O(n+m+q),其中n、m、q分别为点、边、问题数目。本文中为了书写简便,采用的是矩阵的存储方式。

 function find(x:longint):longint;
  begin
    if f[x]<>x then f[x]:=find(f[x]);
    find:=f[x];
  end;
procedure tarjan(u:longint);
  begin
     f[u]:=u; color[u]:=1;
     for i:=1 to n do
     if (g[u,i])and(color[i]=0) then//g[u,i]表示u连着i
        begin
          tarjan(i); f[i]:=u;
        end;
     for i:=1 to n do
     if ((ask[u,i])or(ask[i,u]))and(color[i]=2) then//ask[u,i]表示询问了u,i
       begin
         lca[u,i]:=find(i); lca[i,u]:=lca[u,i];
       end;
     color[u]:=2;
  end;

 

参考例题:Poj 1523、2942、3694、3352、3177  Tyvj P1111 

POJ1523

program p1523;
//割点+dfs判断去割点后联通块
Type
 rec=record
   e,next:longint;
 end;

Var
 top,n,minn,all,i,st,ed,j,ans,no:longint;
 a:array[0..800] of rec;
 b,dfn,low,v:array[0..102] of longint;
 vv:array[0..102] of boolean;

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

Procedure add(st,ed:longint);inline;
  begin
  inc(top);
  with a[top] do
    begin
    e:=ed;
    next:=b[st];
  end;
  b[st]:=top;
end;

procedure clear(var p:array of longint);begin fillchar(p,sizeof(p),0); end;

procedure visit(P:longint);
var
 u:longint;
 y:rec;
  begin
  vv[p]:=true;
  u:=b[p];
  while u<>0 do
    begin
    y:=a[u];
    u:=y.next;
    if not vv[y.e] then visit(y.e);
  end;
end;

procedure dfs(P,pa:longint);
var
 u:longint;
 y:rec;
  begin
  inc(no);
  dfn[p]:=no;
  low[p]:=no;
  v[p]:=1;
  u:=b[p];
  while u<>0 do
    begin
    y:=a[u];
    u:=y.next;
    if y.e<>pa then
    if dfn[y.e]=0 then
      begin
      dfs(y.e,p);
      low[p]:=min(low[p],low[y.e]);
      if low[y.e]>=dfn[p] then inc(v[p]);
    end else low[p]:=min(low[p],dfn[y.e]);
  end;
  if ((p=minn) and (v[p]>2)) or ((p>minn) and (v[p]>1)) then inc(ans);
end;

  begin
  all:=0;
  while true do
    begin
    inc(all);
    fillchar(a,sizeof(a),0);clear(b); top:=0;
    n:=0;minn:=maxlongint;
    while true do
      begin
      read(st);
      if st=0 then break;
      readln(ed);
      n:=max(n,max(st,ed));
      minn:=min(minn,min(st,ed));
      add(st,ed);
      add(ed,st);
    end;
    if top=0 then halt;
    clear(v);clear(dfn);clear(low);
    ans:=0; no:=0;
    dfs(minn,0);
    writeln('Network #',all);

    if ans=0 then writeln('  No SPF nodes') else
      for j:=minn to n do
        if ((j=minn) and (v[j]>2)) or ((j>minn) and (v[j]>1)) then
          begin
          write('  SPF node ',j,' leaves ');    ans:=0;
          fillchar(vv,sizeof(vv),false);
          vv[j]:=true;
          for i:=minn to n do
            if not vv[i] then
              begin
              visit(i);
              inc(ans);
            end;
          writeln(ans,' subnets');
        end;
    writeln;
  end;

end.

 

注:本文部分内容摘自BYVoid神牛的Blog:http://www.byvoid.com/blog/biconnect/

posted on 2013-05-23 20:01  灰天飞雁  阅读(1136)  评论(0编辑  收藏  举报

填写您的邮件地址,订阅我们的精彩内容:  点击这里给我发消息

添加到收藏夹