Business Travel (Codevs 1036)

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. The following lines are N-1, each consisting of two integers a and b (1<=a, b<=n; a<>b), 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 description Output Description

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

Sample input Sample Input
5
1 2
1 5
3 5
4 5
4
1
3
2
5
Sample output Sample Output

7

/*Multiply LCA*/#include<cstdio>#include<iostream>#include<vector>#defineM 30010#defineS 20using namespacestd;intdeep[m],fa[m][s+5],n,m;vector<int>Grap[m];voidDfsintNowint  from,intc) {fa[now][0]= from; Deep[now]=C;  for(intI=0; I<grap[now].size (); i++)      if( from!=Grap[now][i]) DFS (grap[now][i],now,c+1);}voidGet_fa () { for(intj=1; j<=s;j++)       for(intI=1; i<=n;i++) Fa[i][j]=fa[fa[i][j-1]][j-1];}intGet_same (intAintt) {     for(intI=0; i<=s;i++)      if(t& (1<<i)) a=Fa[a][i]; returnA;}intLCA (intAintb) {    if(deep[a]<deep[b]) swap (A, b); A=get_same (a,deep[a]-Deep[b]); if(a==b)returnA;  for(inti=s;i>=0; i--)      if(fa[a][i]!=Fa[b][i]) {a=Fa[a][i]; b=Fa[b][i]; }    returnfa[a][0];}intMain () {scanf ("%d",&N);  for(intI=1; i<n;i++)    {        intx, y; scanf ("%d%d",&x,&y);        Grap[x].push_back (y);    Grap[y].push_back (x); } DFS (1,1,0);    Get_fa (); intp1,p2,ans=0; scanf ("%d%d",&m,&p1);  for(intI=1; i<m;i++) {scanf ("%d",&p2); intzu=LCA (P1,P2); Ans+ = (deep[p1]+deep[p2]-2*Deep[zu]); P1=P2; } printf ("%d", ans); return 0;}
View Code

Business Travel (Codevs 1036)

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.