• 1602: [Usaco2008 Oct]牧场行走(倍增模板)


    var
      sum,ee,q,xx,yy,deep,u,c,v,k,i,j,n,m:longint;
      len,p:array[0..100000,0..20]of longint;
      a,next,long,e,head,d,f:array[0..100000]of longint;
    function lca(a,b:longint):longint;
      var tt,i,j,k:longint;
      begin
      if d[a]>d[b] then begin tt:=a;a:=b;b:=tt;end;
      k:=trunc(ln(d[b])/ln(2));
      for i:=k downto 0 do
        if d[b]-1 shl i>=d[a] then
          begin
          sum:=sum+len[b,i];
          b:=p[b,i];
     
          end;
      if a=b then exit(a);
      k:=trunc(ln(d[b])/ln(2));
      for i:=k downto 0 do
        if p[a,i]<>p[b,i] then
          begin
          sum:=sum+len[b,i]+len[a,i];
          b:=p[b,i];a:=p[a,i];
          end;
      sum:=sum+len[a,0]+len[b,0];
      exit(f[a]);
    end;
    procedure add(u,v,c:longint);
      begin
      inc(ee);
      next[ee]:=head[u];
      head[u]:=ee;
      long[ee]:=c;
      e[ee]:=v;
      inc(ee);
      e[ee]:=u;
      next[ee]:=head[v];
      head[v]:=ee;
      long[ee]:=c;
     
      end;
    procedure doo;
      var h,t,j:longint;
      begin
      a[1]:=1;h:=1;t:=1;
      while h<=t do
      begin
      j:=head[a[h]];
      while j<>0 do
        begin
        if d[e[j]]=0 then
          begin
          d[e[j]]:=d[a[h]]+1;
          f[e[j]]:=a[h];
          len[e[j],0]:=long[j];
          inc(t);
          a[t]:=e[j];
          end;
        j:=next[j];
        end;
      inc(h);
      end;
    end;
     
     
     
    begin
      readln(n,m);
      d[1]:=1;
      for i:=1 to n-1 do
        begin
          readln(u,v,c);
        add(u,v,c);
        end;
      doo;
      for i:=1 to n do
        p[i,0]:=f[i];
      for i:=1 to n do
        if d[i]>deep then
          deep:=d[i];
      k:=trunc(ln(deep)/ln(2));
      for j:=1 to k do
        for i:=1 to n do
            begin
          p[i,j]:=p[p[i,j-1],j-1];
          len[i,j]:=len[i,j-1]+len[p[i,j-1],j-1];
          end;
      for i:=1 to m do
        begin
        sum:=0;
          readln(xx,yy);
        lca(xx,yy);
        writeln(sum);
          end;
        end.
    ------------------------------------------------------------------------- 花有重开日,人无再少年
  • 相关阅读:
    Spring源码学习之容器的基本实现(一)
    面向对象设计原则
    简单易懂带你了解红黑树
    简单易懂带你了解二叉树
    单例模式
    原形模式
    数组与链表
    记一次解决postgresql数据库内存泄露的问题
    记一次排查CPU高的问题
    react ts 设置paths 和 声明非@types的模块
  • 原文地址:https://www.cnblogs.com/lbz007oi/p/2938917.html
Copyright © 2020-2023  润新知