Codevs 1036 Business Travel

Source: Internet
Author: User

Title Description Description

A businessman in a capital city often goes to the towns to do business, and they do it on their own lines 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.

Enter a description Input Description

the first line in the input file has an integer n,1<=n<=30 000, which is the number of towns. N-1 lines below, each line consists of two integers a and b (1<=a, b<=n; a<>b), indicating that town A and town B have road connections. 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 description Output Description

outputs the shortest time the merchant travels in the output file.

LCA problem, can be converted into RMQ problem

Dep[] Indicates the depth of the node in the tree

F is the Euler sequence, and B is the depth of the Euler sequence node.

Pos[] indicates where the node first appears in the Euler sequence

LCA (t,u,v) =F[RMQ (B,pos[u],pos[v])

Here RMQ to return the coordinates, not the concrete value, but the subject does not need, as long as the depth of the LCA can be, directly let RMQ return the specific value, the depth is the return value

The minimum value can also be maintained with a segment tree

1#include <iostream>2#include <cstring>3 using namespacestd;4 Const intmaxn=30001;5 structnode{6     intl,r,mmin;7}tree[8*MAXN];8 structedge{9     intGo,next;Ten}e[2*MAXN]; One intDep[maxn],n,end[maxn],ecount=0, count=0, f[2*maxn],b[2*MAXN],M,POS[MAXN],V[MAXN]; A voidAddintAintb) { -e[++ecount].go=b; -e[ecount].next=End[a]; theend[a]=Ecount; - } - voidBuildtree (intFintXintd) { -     intgo; +dep[x]=D; -f[++count]=x; +b[count]=D; A     if(!V[x]) { atpos[x]=count;v[x]=1; -     } -      for(intI=end[x];i;i=E[i].next) { -go=E[i].go; -         if(go!=f) { -Buildtree (x,go,d+1); inf[++count]=x; -b[count]=D; to         } +     }  - } the voidInit () * { $memset (End,0,sizeof(end));Panax Notoginsengmemset (V,0,sizeof(v)); - } the voidBuildintOintLintR) { +     if(l==R) { ATree[o].l=tree[o].r=l; thetree[o].mmin=B[l]; +         return; -     } $     intM= (L+R)/2; $Build2*O,L,M); Build (2*o+1, m+1, R); -Tree[o].l=l,tree[o].r=R; -Tree[o].mmin=min (tree[o*2].mmin,tree[o*2+1].mmin); the } - intQueryintOintLintR) {Wuyi     if(L&LT;=TREE[O].L&AMP;&AMP;TREE[O].R&LT;=R)returntree[o].mmin; the     intM= (TREE[O].L+TREE[O].R)/2; -     intans=1<< -; Wu     if(l<=m) ans=min (Ans,query (2*o,l,r)); -     if(m<r) ans=min (Ans,query (2*o+1, L,r)); About     returnans; $ } - intMain () - { -Cin>>N; A init (); +     intx, y; the      for(intI=2; i<=n;i++){ -Cin>>x>>y; $ Add (x, y), add (y,x); the     } theBuildtree (-1,1,0);  theBuild1,1, count); the     //for (int i=1;i<=count;i++) cout<<i<< "F:" <<F[i]<<endl; -Cin>>M; in     intlast,ans=0, to; theCin>>Last ; the      for(intI=1; i<m;i++){ AboutCin>>to ; theans+=dep[last]+dep[to]-2*b[query (1, Min (Pos[last],pos[to]), Max (Pos[last],pos[to])); thelast=to ; the     }  +cout<<ans; -     return 0; the}

Codevs 1036 Business Travel

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.