备用交换机 双连通分量

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


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值