[Codevs 1036] Business travel

Source: Internet
Author: User

"Problem 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.

Input

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

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

"Algorithmic Analysis"

Direct use of the Tarjan offline LCA algorithm can pass all the data.

"Program Code"

1#include <cstdio>2#include <cstring>3 using namespacestd;4 intg[30010][510],dis[30010],num[30010],point[30010],n,m,ans=0;5 intfa[30010],que[30010][3];6 BOOLbo[30010];7 voidBuildtree (intu) {8      for(intI=1; i<=num[u];i++)9     if(!Bo[g[u][i]]) {Tenbo[g[u][i]]=true; Onedis[g[u][i]]=dis[u]+1; A Buildtree (G[u][i]); -     } - } the intFindintu) { -     returnfa[u]==u?u:fa[u]=find (Fa[u]); - } - voidTarjan (intu) { +bo[u]=true; -      for(intI=1; i<=que[u][0];i++) +     if(Bo[que[u][i]]) ans+=dis[u]+dis[que[u][i]]-2*Dis[find (Que[u][i]); A      for(intI=1; i<=num[u];i++) at     if(!Bo[g[u][i]]) { - Tarjan (G[u][i]); -fa[g[u][i]]=u; -     } - } - intMain () { inMemset (G,0,sizeof(g)); -memset (NUM,0,sizeof(num)); tomemset (DIS,0,sizeof(DIS)); +memset (Point,0,sizeof(point)); -Memset (Bo,0,sizeof(bo)); thememset (que,0,sizeof(que)); *scanf"%d",&n); $      for(intI=1; i<=n;i++) fa[i]=i;Panax Notoginseng      for(intI=1; i<n;i++){ -         intx, y; thescanf"%d%d",&x,&y); +g[x][++num[x]]=y; Ag[y][++num[y]]=x; the     } +bo[1]=true; dis[1]=0; -Buildtree (1); $scanf"%d",&m); $Memset (Bo,0,sizeof(bo)); -      for(intI=1; i<=m;i++) { -scanf"%d",&point[i]); the         if(i==1&&point[i]==1)Continue; -         Else if(i==1&&point[i]!=1){Wuyique[point[i]][++que[point[i]][0]]=1; theque[1][++que[1][0]]=Point[i]; -         } Wu         Else{ -que[point[i]][++que[point[i]][0]]=point[i-1]; Aboutque[point[i-1]][++que[point[i-1]][0]]=Point[i]; $         } -     } -Tarjan (1); -printf"%d", ans); A     return 0; +}

Disclaimer: This article is the original blogger, do not reprint without permission.

[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.