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.
    



  • 相关阅读:
    python 发包爬取中国移动充值页面---可判断手机号是否异常
    python利用selenium和safari浏览器驱动实现新浪微博自动点赞 Demo
    Django学习报错记录
    nginx和tomcat的区别
    Mac主机映射到域名
    mac下eclipse安装svn插件-subclipse
    移动端——等分,居中
    移动端——重置样式
    M端页面-绝对定位布局
    jquery-练习-折叠效果
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319673.html
Copyright © 2011-2022 走看看