备用交换机

来源:互联网 发布:商超软件排名 编辑:程序博客网 时间:2024/05/23 17:34

题目:备用交换机

问题描述

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

【输入格式】

输入文件有若干行

第一行,一个整数n,表示共有n个城市(2<=n<=100)

下面有若干行,每行2个数a、b,a、b是城市编号,表示a与b之间有直接通讯线路。

【输出格式】

输出文件有若干行

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

【输入输出样例】

输入文件名: gd.in

7

1 2

2 3

2 4

3 4

4 5

4 6

4 7

5 6

6 7

输出文件名:gd.out

2

2

4

 

题解:求一个无向图的所有割点(大神方法)。

标程:

const

 maxn=100;

 maxv=2000;

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;

procedureadd(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;

 

procedure init;

var

  x,y:longint;

begin

 read(n);

 while not seekeof do

  begin

   read(x,y);

   add(x,y);

  end;

end;

 

functionmin(x,y:longint):longint;

begin

 if x<y then exit(x)

        else exit(y);

end;

 

proceduredfs(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);

 for i:=1 to n do

  if b[i] then writeln(i);

end;          

 

begin

 assign(input,'gd.in');

 assign(output,'gd.out');

 reset(input);

 rewrite(output);

 init;

 tarjan;

 print;

 close(input);

 close(output);

end.

 

1 0
原创粉丝点击