当前位置:首页 > 服务端 > toj1026 Network 双连通分量

toj1026 Network 双连通分量

题意/Description:

    A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.


读入/Input

        The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;


输出/Output

        The output contains for each block except the last in the input file one line containing the number of critical places.


题解/solution

        改一下输入(输入十分神奇),水水的连通图。详情见:

 

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;

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);
end;

procedure init;
var
  i,x,y:longint;
begin
  readln(n);
  while n<>0 do
    begin
      cnt:=0; tot:=0;
      fillchar(dfn,sizeof(dfn),0);
      fillchar(low,sizeof(low),0);
      fillchar(tu,sizeof(tu),0);
      fillchar(ls,sizeof(ls),0);
      fillchar(ans,sizeof(ans),0);
      read(x);
      while x<>0 do
        begin
          while not eoln do
            begin
              read(y);
              add(x,y);
            end;
          read(x);
        end;
      tarjan(1);
      print;
      readln(n);
    end;
end;

begin
  init;
end



 

作者:猪都哭了
来源链接:https://www.cnblogs.com/zyx-crying/p/9319671.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125198.html

分享给朋友: