+lca "Pascal" _lca, the largest spanning tree of noip2013 wagon transportation

Source: Internet
Author: User

The bare maximum spanning tree +lca=. =, two templates just a little bit better =w=

Type Rec=record ll,rr,len:longint;

End
    var n,m,l:longint;
    I,j:longint;
    A:ARRAY[0..50010] of rec;
    F,D,LAST:ARRAY[0..10010] of Longint;
    FLAG:ARRAY[0..10010] of Boolean;
    PRE,OTHER,LEN:ARRAY[0..100010] of Longint;
DIS,JUMP:ARRAY[0..10010,0..20] of Longint;
function min (a,b:longint): Longint;
Begin if A<b then exit (a) Else exit (b);
End
Procedure swap (Var a,b:longint);
var c:longint;
Begin C:=a;a:=b;b:=c;
End
Procedure connect (X,y,z:longint);
   Begin Inc (L);
   PRE[L]:=LAST[X];
   Last[x]:=l;
   Other[l]:=y;
Len[l]:=z;

End
Procedure Dfs (X:longint);
var p,q:longint;
   Begin Q:=LAST[X];
      while (q<>0) do begin P:=OTHER[Q];
         If not flag[p] then begin flag[p]:=true;
         d[p]:=d[x]+1;
         Jump[p,0]:=x;
         DIS[P,0]:=LEN[Q];
      DFS (p);
      End
   Q:=PRE[Q];
End EnD
Procedure sort (l,r:longint);
         var i,j,x:longint;
Y:rec;
         Begin I:=l;
         J:=r;
         x:=a[(l+r) div 2].len;
           Repeat while A[i].len<x do Inc (i);
           While X<a[j].len do Dec (j);
                If not (I&GT;J) then begin y:=a[i];
                A[I]:=A[J];
                A[j]:=y;
                Inc (I);
             J:=j-1;
         End
         Until i>j;
         If L<j then sort (l,j);
If I<r then sort (i,r);
End
function Get_father (x:longint): Longint;
    Begin if x=f[x] then exit (x);
    F[x]:=get_father (F[x]);
Exit (F[x]);

End
Procedure Max_tree;
    var t,aa,b:longint;
I:longint;
   Begin t:=0;
      For i:=m Downto 1 does begin if (t=m-1) then break;
      Aa:=get_father (A[I].LL);
      B:=get_father (A[I].RR);
         if (aa<>b) then begin F[B]:=AA;
         Inc (T);
         Connect (A[i].ll,a[i].rr,a[i].len); ConnectA[i].rr,a[i].ll,a[i].len);
   End
End
End
Procedure LCA;
    var ans,x,y:longint;
I,j:longint;
   Begin read (x,y);
      if (Get_father (x) <>get_father (y)) THEN begin Writeln (-1);
   Exit
   End
   Ans:=maxlongint;
   if (D[x]>d[y]) then swap (x,y);
       For j:=0 to do if ((1<<j) and (d[y]-d[x)) <>0 then begin Ans:=min (ANS,DIS[Y,J));
    Y:=JUMP[Y,J];
    End if (x<>y) then begin for j:=15 Downto 0 does if (jump[x,j]<>jump[y,j)) then be
           Gin Ans:=min (ans,dis[x,j]);
           Ans:=min (Ans,dis[y,j]);
           X:=JUMP[X,J];
        Y:=JUMP[Y,J];
        End
        Ans:=min (ans,dis[y,0]);
    Ans:=min (ans,dis[x,0]);
    End
Writeln (ANS);

End
   Begin read (N,M);
   For I:=1 to n do f[i]:=i;
   For I:=1 to M do read (A[i].ll,a[i].rr,a[i].len);
   Sort (1,m);
   Max_tree;
   D[1]:=1;
   Flag[1]:=true;
   DFS (1);
    For J:=1 TodoFor I:=1 to n do begin jump[i,j]:=jump[jump[i,j-1],j-1];
    Dis[i,j]:=min (Dis[i,j-1],dis[jump[i,j-1],j-1]);
   End
   Read (m);
For I:=1 to M do LCA;
 End.
--by Eirlys


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.