zoukankan      html  css  js  c++  java
  • 备用交换机 双连通分量

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



  • 相关阅读:
    随机数生成
    C#根据流下载文件
    C# 改变Windows中服务的登录身份 (账户名和密码)
    SqlServer查看数据库信息及服务器级、数据库级、数据库独立 用户权限
    C# 两个List<T>(T是类)使用Contains比较元素的方法
    你真正了解public、internal、protected、private吗?不妨幽默一些
    【WCF Restful】Post传参示范
    VMware虚拟机可与Win10物理机互ping并可访问互联网的设置方法
    服务器Oracle数据库配置与客户端访问数据库的一系列必要设置
    Winform ListBox输出信息并自动滚动至底部
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319675.html
Copyright © 2011-2022 走看看