备用交换机 双连通分量

题意/Description:

    n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间接与其它城市连接。因电子设备容易损坏,需给通讯点配备备用交换机。但备用交换机数量有限,不能全部配备,只能给部分重要城市配置。于是规定:如果某个城市由于交换机损坏,不仅本城市通讯中断,还造成其它城市通讯中断,则配备备用交换机。请你根据城市线路情况,计算需配备备用交换机的城市个数,及需配备备用交换机城市的编号。

 

读入/Input

    输入文件有若干行
    第一行,一个整数n,表示共有n个城市(2<=n<=100)
下面有若干行,每行2个数a、b,a、b是城市编号,表示a与b之间有直接通讯线路。

 

输出/Output

    输出文件有若干行
    第一行,1个整数m,表示需m个备用交换机,下面有m行,每行有一个整数,表示需配备交换机的城市编号,输出顺序按编号由小到大。如果没有城市需配备备用交换机则输出0。

 

题解/solution

    tarjan算法做双连通分量,详情请见

http://www.cnblogs.com/en-heng/p/4002658.html

好好理解吧!

 

 

代码/Code

 

type
  arr=record
    y,next,op:longint;
    f:boolean;
  end;
var
  dfn,low,ls:array [0..101] of longint;
  tu:array [0..1001] of arr;
  ans:array [0..101] of boolean;
  n,m,tot,p,cnt:longint;
procedure add(o,p:longint);
begin
  inc(tot);
  with tu[tot] do
    begin
      y:=p; op:=tot+1;
      next:=ls[o];
      ls[o]:=tot;
    end;
  inc(tot);
  with tu[tot] do
    begin
      y:=o; op:=tot-1;
      next:=ls[p];
      ls[p]:=tot;
    end;
end;

procedure init;
var
  i,x,y:longint;
begin
  readln(n);
  while not seekeof do
    begin
      readln(x,y);
      add(x,y);
    end;
end;

function min(o,p:longint):longint;
begin
  if o<p then exit(o);
  exit(p);
end;

procedure tarjan(x:longint);
var
  t:longint;
begin
  inc(p);
  dfn[x]:=p; low[x]:=p;
  t:=ls[x];
  while t>0 do
    with tu[t] do
      begin
        if not f then
          begin
            f:=true;
            tu[op].f:=true;
            if dfn[y]=0 then
              begin
                if x=1 then inc(cnt);
                tarjan(y);
                low[x]:=min(low[x],low[y]);
                if low[y]>=dfn[x] then ans[x]:=true;
              end else
              low[x]:=min(low[x],dfn[y]);
          end;
        t:=next;
      end;
end;

procedure print;
var
  i,sum:longint;
begin
  if cnt>=2 then ans[1]:=true
            else ans[1]:=false;
  sum:=0;
  for i:=1 to n do
    if ans[i] then inc(sum);

  writeln(sum);
  for i:=1 to n do
    if ans[i] then writeln(i);
end;

begin
  init;
  tarjan(1);
  print;
end.



 

posted @ 2016-06-24 21:36  猪都哭了  阅读(200)  评论(0编辑  收藏  举报