割边:
Program FAFU1180; type edge=record v,n:longint; end; const maxn=101; maxm=20001; var e:array[0..maxm] of edge; h:array[0..maxn] of longint; dfn,low:array[0..maxn] of longint; o,n,m,u,v,ans,idx,cnt,root:longint; function min(a,b:longint):longint; begin if a>b then exit(b); exit(a); end; procedure dfs(a,u:longint); var v,p:longint; begin inc(idx); dfn[u]:=idx; low[u]:=idx; p:=h[u]; while p<>0 do begin v:=e[p].v; if dfn[v]=-1 then begin dfs(u,v); low[u]:=min(low[u],low[v]); if low[v]>dfn[u] then inc(ans); end else if v<>a then low[u]:=min(low[u],dfn[v]); p:=e[p].n; end; end; procedure Tarjan; var o:longint; f:boolean; begin fillchar(dfn,sizeof(dfn),255); fillchar(low,sizeof(low),255); f:=true; for o:=1 to n do if dfn[o]=-1 then begin idx:=0; root:=o; dfs(0,root); f:=not f; if f then begin ans:=-1; exit; end; end; end; procedure add(u,v:longint); begin inc(cnt); e[cnt].v:=v; e[cnt].n:=h[u]; h[u]:=cnt; end; begin while not seekeof do begin readln(n,m); cnt:=0; fillchar(h,sizeof(h),0); for o:=1 to m do begin read(u,v); add(u,v); add(v,u); end; readln; ans:=0; Tarjan; writeln(ans); end; end.
割点:
Program POJ1144; type edge=record v,n:longint; end; const maxn=101; maxm=500; var e:array[0..maxm] of edge; h:array[0..maxn] of longint; dfn,low:array[0..maxn] of longint; flag:array[0..maxn] of boolean; o,n,u,v,ans,idx,cnt,root:longint; function min(a,b:longint):longint; begin if a>b then exit(b); exit(a); end; procedure dfs(a,u:longint); var v,p,num:longint; begin inc(idx); dfn[u]:=idx; low[u]:=idx; p:=h[u]; num:=0; while p<>0 do begin v:=e[p].v; if dfn[v]=-1 then begin dfs(u,v); inc(num); low[u]:=min(low[u],low[v]); if ((u=root)and(num=2))or ((u<>root)and(low[v]>=dfn[u])) then flag[u]:=true; end else if v<>a then low[u]:=min(low[u],dfn[v]); p:=e[p].n; end; end; procedure Tarjan; var o:longint; begin fillchar(flag,sizeof(flag),0); fillchar(dfn,sizeof(dfn),255); fillchar(low,sizeof(low),255); for o:=1 to n do if dfn[o]=-1 then begin idx:=0; root:=o; dfs(0,root); end; end; procedure add(u,v:longint); begin inc(cnt); e[cnt].v:=v; e[cnt].n:=h[u]; h[u]:=cnt; end; begin while not seekeof do begin readln(n); if n=0 then break; cnt:=0; fillchar(h,sizeof(h),0); read(u); while u<>0 do begin while not eoln do begin read(v); add(u,v); add(v,u); end; readln; read(u); end; readln; Tarjan; ans:=0; for o:=1 to n do if flag[o] then inc(ans); writeln(ans); end; end.