题目内容
n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间接与其它城市连接。因电子设备容易损坏,需给通讯点配备备用交换机。但备用交换机数量有限,不能全部配备,只能给部分重要城市配置。于是规定:如果某个城市由于交换机损坏,不仅本城市通讯中断,还造成其它城市通讯中断,则配备备用交换机。请你根据城市线路情况,计算需配备备用交换机的城市个数,及需配备备用交换机城市的编号。
分析
分析可得,如果某个城市由于交换机损坏,不仅本城市通讯中断,还造成其它城市通讯中断,那这个城市一定是无向图割点。所以就是求所有的断点。
求断点:http://www.byvoid.com/blog/biconnect/
代码
const
maxe=50000;
maxv=1000;
type
rec=record
x,y,w,next:longint;
flag:boolean;
end;
var
n,m:longint;
a:array[1..maxe] of rec;
ls:array[1..maxv] of longint;
ans:array[1..maxe] of longint;
low,dfn:array[1..maxv] of longint;
tot,num,ans1:longint;
i,j,k:longint;
root:longint;
procedure dfs(r:longint);
var
i,j,k:longint;
begin
tot:=tot+1;
low[r]:=tot;
dfn[r]:=tot;
i:=ls[r];
while i<>0 do
with a[i] do
begin
if not flag
then begin
flag:=true;
a[w].flag:=true;
if dfn[y]=0
then
begin
if r=root
then num:=num+1;
dfs(y);
if low[r]>low[y]
then low[r]:=low[y];
if low[y]>=dfn[r]
then ans[r]:=1;
end
else
if low[r]>dfn[y]
then low[r]:=dfn[y];
end;
i:=next;
end;
end;
procedure add(x,y:longint);
begin
m:=m+1;
a[m].x:=x;
a[m].y:=y;
a[m].w:=m+1;
a[m].next:=ls[x];
ls[x]:=m;
m:=m+1;
a[m].x:=y;
a[m].y:=x;
a[m].w:=m-1;
a[m].next:=ls[y];
ls[y]:=m;
end;
begin
assign(input,'gd.in');
assign(output,'gd.out');
reset(input);
rewrite(output);
readln(n);
m:=0; num:=0;
while not eof do
begin
readln(j,k);
add(j,k);
end;
root:=1;
dfs(1);
if num>=2
then
ans[root]:=1
else
ans[root]:=0;
ans1:=0;
for i:=1 to n do
if ans[i]=1 then ans1:=ans1+1;
writeln(ans1);
for i:=1 to n do
if ans[i]=1
then writeln(i);
close(input);
close(output);
end.