zoukankan      html  css  js  c++  java
  • 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.
    



  • 相关阅读:
    游千佛塔有感
    时刻坚持高标准:成大事者的十条“箴言”
    谁愿意嫁给我这样的人
    成功的秘诀之一,就是敢于提出大设想、大思考
    寒冬里的暖阳
    世界最伟大的管理原则
    把你藏在心里
    登天门有感
    办公室保持最佳状态的诀窍
    “领悟”的价值是什么?思维能力训练问答
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319673.html
Copyright © 2011-2022 走看看