您的位置:首页 > 其它

poj1144 割点模板

2016-11-15 17:13 176 查看
tarjan求割点(low[p]>=dfn[x]) 的模板题,注意对dfs序列根节点情况的单独考虑(必须访问至少两次)

var
n,ans,l,x,y     :longint;
time,son        :longint;
last,low,dfn    :array[0..110] of longint;
vis,flag        :array[0..110] of boolean;
pre,other       :array[0..20010] of longint;
i               :longint;
procedure connect(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
end;

function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;

procedure dfs(x:longint);
var
p,q:longint;
begin
inc(time);
low[x]:=time;
dfn[x]:=time;
vis[x]:=true;
//
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if dfn[p]=0 then
begin
dfs(p);
low[x]:=min(low[x],low[p]);
if (low[p]>=dfn[x]) and (x<>1) then flag[x]:=true
else if (x=1) and (low[p]>=dfn[x]) then inc(son);
end else
if vis[p] then low[x]:=min(low[x],dfn[p]);
q:=pre[q];
end;
end;

begin
read(n);
while (n<>0) do
begin
l:=0;ans:=0;son:=0;time:=0;
fillchar(last,sizeof(last),0);
fillchar(dfn,sizeof(dfn),0);
fillchar(low,sizeof(low),0);
fillchar(vis,sizeof(vis),false);
fillchar(flag,sizeof(flag),false);
read(x);
while (x<>0) do
begin
while not eoln do
begin
read(y);
connect(x,y);
connect(y,x);
end;
read(x);
end;
for i:=1 to n do if dfn[i]=0 then dfs(i);
if (son>1) then inc(ans);
for i:=1 to n do if flag[i] then inc(ans);
writeln(ans);
read(n);
end;
end.


——by Eirlys
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模板 tarjan