zoukankan      html  css  js  c++  java
  • bzoj1832

    其实这道题是和bzoj1787一样的
    但我用bzoj1787MLE了,于是正好练一下树上倍增

      1 type node=record
      2        po,next:longint;
      3      end;
      4 
      5 var w:array[0..1000010] of node;
      6     anc:array[0..500005,0..20] of longint;
      7     dep,fa,p:array[0..500005] of longint;
      8     v:array[0..500005] of boolean;
      9     t,len,x,y,z,a,b,c,i,n,m:longint;
     10 
     11 procedure add(x,y:longint);
     12   begin
     13     inc(len);
     14     w[len].po:=y;
     15     w[len].next:=p[x];
     16     p[x]:=len;
     17   end;
     18 
     19 procedure swap(var a,b:longint);
     20   var c:longint;
     21   begin
     22     c:=a;
     23     a:=b;
     24     b:=c;
     25   end;
     26 
     27 procedure dfs(x:longint);
     28   var i,y:longint;
     29   begin
     30     v[x]:=true;
     31     i:=p[x];
     32     while i<>0 do
     33     begin
     34       y:=w[i].po;
     35       if not v[y] then
     36       begin
     37         fa[y]:=x;
     38         dep[y]:=dep[x]+1;
     39         dfs(y);
     40       end;
     41       i:=w[i].next;
     42     end;
     43   end;
     44 
     45 procedure prework;
     46   var i,j,h:longint;
     47   begin
     48     fillchar(anc,sizeof(anc),255);
     49     h:=0;
     50     for i:=1 to n do
     51     begin
     52       anc[i,0]:=fa[i];
     53       if dep[i]>h then h:=dep[i];
     54     end;
     55     t:=trunc(ln(h)/ln(2));
     56     for j:=1 to t do
     57       for i:=1 to n do
     58       begin
     59         b:=anc[i,j-1];
     60         if anc[b,j-1]<>-1 then anc[i,j]:=anc[b,j-1];
     61       end;
     62   end;
     63 
     64 function lca(x,y:longint):longint;
     65   var i,p:longint;
     66   begin
     67     if dep[x]<dep[y] then swap(x,y);
     68     if dep[x]<>dep[y] then
     69     begin
     70       p:=trunc(ln(dep[x])/ln(2));
     71       for i:=p downto 0 do
     72         if dep[x]-1 shl i>=dep[y] then
     73         begin
     74           x:=anc[x,i];
     75           if dep[x]=dep[y] then break;
     76         end;
     77     end;
     78 
     79     if x=y then exit(y);
     80     p:=trunc(ln(dep[x])/ln(2));
     81     for i:=p downto 0 do
     82       if (anc[x,i]<>-1) and (anc[x,i]<>anc[y,i]) then
     83       begin
     84         x:=anc[x,i];
     85         y:=anc[y,i];
     86       end;
     87     exit(fa[x]);
     88   end;
     89 
     90  begin
     91   readln(n,m);
     92   for i:=1 to n-1 do
     93   begin
     94     readln(x,y);
     95     add(x,y);
     96     add(y,x);
     97   end;
     98   fa[1]:=-1;
     99   dfs(1);
    100   prework;
    101   for i:=1 to m do
    102   begin
    103     readln(x,y,z);
    104     a:=lca(x,y);
    105     b:=lca(x,z);
    106     c:=lca(y,z);
    107     if a=b then
    108       writeln(c,' ',dep[y]+dep[z]-2*dep[c]+dep[c]+dep[x]-2*dep[lca(x,c)])
    109     else if a=c then
    110       writeln(b,' ',dep[x]+dep[z]-2*dep[b]+dep[b]+dep[y]-2*dep[lca(y,b)])
    111     else if b=c then
    112       writeln(a,' ',dep[x]+dep[y]-2*dep[a]+dep[a]+dep[z]-2*dep[lca(z,a)])
    113   end;
    114 end.
    115 
    116  
    View Code
  • 相关阅读:
    BZOJ 3330 分数
    FR #11题解
    BZOJ 1857 传送带
    BZOJ 4757 Building a Tall Barn
    FR #10题解
    BZOJ 4393 Fruit Feast
    BZOJ 3126 Photo
    BZOJ 1312 Hard Life
    BZOJ 2039 employ人员雇佣
    Count SIN Numbers
  • 原文地址:https://www.cnblogs.com/phile/p/4473118.html
Copyright © 2011-2022 走看看