poj 1144 Network 无向图求割点

题目内容

  就是给你一些点,某些点之间有边。求有多少个点是割点。

分析

  模板题目了,直接用无向图求个点模板就可以ac。需要注意的是输入格式。

 

代码

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
  readln(n);
  while n<>0 do begin
  fillchar(dfn,sizeof(dfn),0);
  fillchar(low,sizeof(low),0);
  fillchar(a,sizeof(a),0);
  fillchar(ls,sizeof(ls),0);
  fillchar(ans,sizeof(ans),0);
  tot:=0;
  read(j); num:=0;
  while j<>0 do
    begin
      while not eoln do
        begin
          read(k);
          add(j,k);
        end;
      readln;
      read(j);
    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);
  readln(n);
  end;
  close(input);
  close(output);
end.




posted @ 2016-06-25 09:41  一个响亮的蒟蒻  阅读(105)  评论(0编辑  收藏  举报