poj 1144 (tju 1026)

Description

ATelephone Line Company (TLC) is establishing a new telephone cable network.They are connecting several places numbered by integers from 1 to N . No twoplaces have the same number. The lines are bidirectional and always connecttogether 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 adirect connection, it can go through several exchanges. From time to time thepower supply fails at a place and then the exchange does not operate. Theofficials from TLC realized that in such a case it can happen that besides thefact that the place with the failure is unreachable, this can also cause thatsome other places cannot connect to each other. In such a case we will say theplace (where the failure 
occured) is critical. Now the officials are trying to write a program forfinding the number of all such critical places. Help them.

Input

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

Output

The outputcontains for each block except the last in the input file one line containingthe number of critical places.

Sample Input(简析)

5            (5个点)

5 1 2 3 4 (5号点与1,2,3,4有双向边,后面有若干个数)

0             (每组数据以0结束)

6

2 1 3

5 4 6 2

0

0         (输入以一个0结束)

Sample Output

1

2

Hint

You needto determine the end of one line.In order to make it's easy to determine,thereare no extra blank before the end of each line.

 

分析:求图的割点数目。

 

const
 maxn=1000;
 maxv=20000;
type
 node=record
 y,other,next:longint;
 f:boolean;
end;
 
var
 g:array [1..maxv] of node;
 dfn,low,ls:array [1..maxn] of longint;
 b:array [1..maxn] of boolean;
 n,p,e,v:longint;
procedure add(x,y:longint);
 begin
 inc(e);
 g[e].y:=y;
 g[e].other:=e+1;
 g[e].next:=ls[x];
 ls[x]:=e;
 inc(e);
 g[e].y:=x;
 g[e].other:=e-1;
 g[e].next:=ls[y];
 ls[y]:=e;
 end;
 
function min(x,y:longint):longint;
begin
 if x<y then exit(x)
       else exit(y);
end;
 
procedure dfs(x:longint);
 var
 t:longint;
begin
 inc(p);
 dfn[x]:=p;
 low[x]:=p;
 t:=ls[x];
 while t>0 do
 with g[t] do
  begin
   if not f then
    begin
     f:=true;
     g[other].f:=true;
     if dfn[y]=0 then
      begin
       if x=1 then inc(v);
       dfs(y);
       low[x]:=min(low[x],low[y]);
       if low[y]>=dfn[x] then b[x]:=true;
      end
      else
       low[x]:=min(low[x],dfn[y]);
    end;
   t:=next;
  end;
end;
 
procedure tarjan;
 begin
 dfs(1);
  if v>1 then b[1]:=true else b[1]:=false;
 end;
 
procedure print;
 var i,ans:longint;
begin
 ans:=0;
 for i:=1 to n do
  if b[i] then inc(ans);
 writeln(ans);
end;
 
procedure init;
var
 x,y:longint;
begin
 repeat
 fillchar(ls,sizeof(ls),0);
 e:=0; p:=0; v:=0;
 fillchar(g,sizeof(g),0);
 fillchar(dfn,sizeof(dfn),0);
 fillchar(low,sizeof(low),0);
 fillchar(b,sizeof(b),false);
 readln(n);
  if n=0 then break;
  repeat
   read(x);
   if x=0 then break;
   while not eoln do
    begin
     read(y);
     add(x,y);
    end;
  until false;
 tarjan;
 print;
 until false;
end;
 
 
begin
 init;
end.

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值