2953: [Poi2002] Business travel time limit:3 Sec Memory limit:128 MB
Submit:8 Solved:8
[Submit] [Status] Description businessmen in a capital city often go to towns and cities to do business, and they do it on their own lines, in order to save time. Suppose there are N towns, the capital is numbered 1, the merchant departs from the capital, there are road connections between the other towns, and if there are direct links between any two towns, it takes a unit of time to travel between them. The country has a well-developed road network, which can reach any town from the capital, and the road network will not be a ring. Your task is to help the businessman calculate his shortest travel time. Inputthe first line has an integer n,1<=
N<=30 000, for the number of towns. N-1 line below, each line consists of two integers
aAnd
b(1<=
a,
b<=n; A<>B), which indicates a road connection between Town A and town B. In the N+1 Act an integer m, the following M-line, each line has the number of towns that the merchant needs to pass sequentially. Output outputs the shortest time the merchant travels. Sample Input5
1 2
1 5
3 5
4 5
4
1
3
2
5
Sample Output
7
HINT Source
The problem: Originally wanted to do a line segment tree topic in Codevs, but into the line segment tree classification found this (hansbug: hehe, this is also called Line segment tree I am also drunk phile: ye Konjac konjac dare to rob my lines? Huh, huh? Hansbug: I was wrong ^_^) as long as you learn the LCA of children's shoes is not difficult to find this is a most naked LCA, directly on the multiplication of the line, coupled with the side of the long array are exempt ...
1 type2Point=^node;3Node=Record4 G:longint;5 Next:point;6 End;7 8 var9 I,j,k,l,m,n,t:longint;TenA:Array[0..50000] ofPoint ; OneC:Array[0.. -,0..50000] ofLongint; AD:Array[0..50000] ofLongint; - functionmin (x,y:longint): Longint;inline; - begin the ifX<y ThenMin:=xElsemin:=y; - End; - functionMax (x,y:longint): Longint;inline; - begin + ifX>y ThenMax:=xElsemax:=y; - End; + procedureSwapvarx,y:longint); inline; A varZ:longint; at begin -z:=x;x:=y;y:=Z; - End; - procedureAdd (x,y:longint); inline; - varP:point; - begin in new (p); -p^.g:=y; top^.next:=A[x]; +a[x]:=p; - End; the procedureDFS (x:longint); inline; * varP:point; $ beginPanax Notoginsengp:=A[x]; - whileP<>Nil Do the begin + ifc[0, p^.g]=0 Then A begin thec[0, p^.g]:=x; +d[p^.g]:=d[x]+1; - DFS (P^.G); $ End; $p:=P^.next; - End; - End; the functionGetfat (x,y:longint): Longint;inline; - varI,j,k:longint;Wuyi begin thei:=0; - whileY>0 Do Wu begin - ifOdd (Y) Thenx:=C[i,x]; AboutInc (i); y:=yDiv 2; $ End; -getfat:=x; - End; - functionDis (x,y:longint): Longint; A var + A1,a2,a3,i,j,k,l:longint; the begin - ifD[x]<d[y] Thenswap (x, y); $a1:=x;a2:=y; theX:=getfat (x,d[x]-d[y]); the ifX=y ThenExit (d[a1]-d[a2]); the fori:= - Downto 0 Do the begin - ifc[i,x]=0 Thencontinue; in ifC[i,x]<>c[i,y] Then the begin thex:=C[i,x]; Abouty:=C[i,y]; the End the End; thea3:=c[0, x]; +Exit (d[a1]+d[a2]-d[a3]-d[a3]); - End; the Bayi begin the READLN (n); the fori:=1 toN Doa[i]:=Nil; - fori:=1 toN-1 Do - begin the readln (j,k); the Add (j,k); add (k,j); the End; theFillchar (C,sizeof (c),0); -Fillchar (d,sizeof (d),0); thec[0,1]:=-1; theDfs1); c[0,1]:=0; the fori:=1 to - Do94 forj:=1 toN Do thec[i,j]:=c[i-1, c[i-1, J]]; the Readln (m); theReadln (j); l:=0;98 fori:=1 toM-1 Do About begin -k:=J;101 Readln (j);102t:=dis (k,j);103l:=l+T;104 End; the Writeln (l);106 Readln;107 End.108
2953: [Poi2002] Business trip