poj3177 Redundant Paths 双连通分量

题意/Description:

       In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 
       Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 
       There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.


读入/Input

       Line 1: Two space-separated integers: F and R 
       Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.


输出/Output

       Line 1: A single integer that is the number of new paths that must be built.


题解/solution

    首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。(注意判断重边)

(不懂双连通分量见http://www.cnblogs.com/en-heng/p/4002658.html


代码/Code

type
  arr=record
    x,y,next:longint;
  end;
var
  dfn,low,ls,ttt:array [0..5001] of longint;
  a:array [0..5001,0..5001] of boolean;
  tu:array [0..50001] of arr;
  ans:array [0..5001] of boolean;
  n,m,tot,p,cnt:longint;
procedure add(o,p:longint);
begin
  inc(tot);
  with tu[tot] do
    begin
      x:=o; y:=p;
      next:=ls[o];
      ls[o]:=tot;
    end;
  inc(tot);
  with tu[tot] do
    begin
      x:=p; y:=o;
      next:=ls[p];
      ls[p]:=tot;
    end;
end;

function min(o,p:longint):longint;
begin
  if o<p then exit(o);
  exit(p);
end;

procedure tarjan(x,fa:longint);
var
  t:longint;
begin
  inc(p);
  dfn[x]:=p; low[x]:=p;
  t:=ls[x];
  while t>0 do
    with tu[t] do
      begin
        if dfn[y]=0 then
          begin
            tarjan(y,x);
            low[x]:=min(low[x],low[y]);
          end else
          if y<>fa then low[x]:=min(low[x],dfn[y]);
        t:=next;
      end;
end;

procedure print;
var
  i,sum:longint;
begin
  for i:=1 to tot do
    with tu[i] do
      if low[x]<>low[y] then inc(ttt[low[x]]);
  sum:=1;
  for i:=1 to n do
    if ttt[i]=1 then inc(sum);
  writeln(sum div 2);
end;

procedure init;
var
  i,x,y:longint;
begin
  fillchar(a,sizeof(a),true);
  readln(n,m);
  for i:=1 to m do
    begin
      readln(x,y);
      if not a[x,y] then continue;
      a[x,y]:=false; a[y,x]:=false;
      add(x,y);
    end;
end;

begin
  init;
  tarjan(1,1);
  print;
end.


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值