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.
Dull Direct Hair code, I can not see it, so I decided to talk about the practice, really do not come out, and then copy my code below it;
First we should know how to solve, the route is as follows:
1-->3-->2-->5 and N can reach 30000, if each point is used SPFA to find the shortest distance and then accumulate, definitely timed out
Because there is no ring in the diagram, there must be only one route between two points, which is done with LCA (if you can also use line tree, will teach me)
That is, only ask for 1->1 (1->1 is afraid that the data has the beginning of the first point is not reached 1), 1->3,3->2,2->5 's recent public ancestor, and then to each vertex is the nearest common ancestor distance and can be calculated;
If you don't understand it's okay, let me give you a little simulation of the sample.
1
/ \
2 5
/ \
3 4
Figure on top
1. First use the BFS to calculate the distance from Vertex 1 to each vertex, using the DIS array to represent:
1 2 3) 4 5
Dis 0 1 2 2 1
The use of this distance, for example: 2 and 5 of the nearest public ancestor is 1, and 2 to 5 distance is dis[2]-dis[1]+dis[5]-dis[1]= (dis[2]+dis[5]) -2*dis[1]=2, is the distance from 2 to the public ancestor plus 5 to the public ancestor.
2. Then the LCA practice of self-Baidu, calculate the 1--1,1--3,3--2,2--5 of the public ancestor, just 1 (,,,) and then calculate (Dis[1]+dis[1]) -2*dis[1]+ (dis[1]+dis[3]) -2*dis[1]+ ( DIS[3]+DIS[2]) -2*dis[1]+ (dis[2]+dis[5]) -2*dis[1] That's the answer.
Finally, there is no understanding of the contact [email protected], also welcome to correct the mistake, more welcome hope God Ben to take me to fly
Codevs on the same question I wrote, do not suspect I copied:)
1#include <cstdio>2#include <iostream>3#include <vector>4#include <queue>5#include <cstring>6 using namespacestd;7 Const int_maxn=30010;8vector<int>A[_MAXN];9vector<int>T[_MAXN];Tenqueue<int>que; One intDIS[_MAXN],N,M,F[_MAXN]; A BOOLVIS[_MAXN]; - structquestion - { the intU,v,ans; - }B[_MAXN]; - voidRead () - { + inti,x,y; -scanf"%d",&n); + for(i=1; i<n;i++) A { atf[i]=i; -scanf"%d%d",&x,&y); - a[x].push_back (y); - a[y].push_back (x); - } -f[n]=N; inscanf"%d",&m); -b[1].u=1; to for(i=1; i<=m;i++) + { -scanf"%d",&x); theb[i].v=x; * T[x].push_back (i); $ if(i+1<=m)Panax Notoginseng { -b[i+1].u=x; theT[x].push_back (i+1); + } A } the } + intFindintx) - { $ returnF[x]==x?X:find (f[x]); $ } - voidLcaintXintFA) - { the intL=a[x].size (), i,to; - for(i=0; i<l;i++)Wuyi { theto=A[x][i]; - if(to==FA) Wu Continue; - LCA (to,x); Aboutf[to]=x; $vis[to]=true; - } -L=t[x].size (); - intXx,yy,zz; A for(i=0; i<l;i++) + { theto=T[x][i]; -xx=b[to].u; $yy=b[to].v; the if(xx==yy) theb[to].ans=xx; the if(Xx!=x && yy==x &&Vis[xx]) theb[to].ans=find (XX); - if(Xx==x && yy!=x &&Vis[yy]) inb[to].ans=find (yy); the } the } About voidBFS () the { thememset (Vis,false,sizeof(Vis)); theQue.push (1); +vis[1]=true; -dis[1]=0; the intIl from, to;Bayi while(!que.empty ()) the { the from=Que.front (); - Que.pop (); -l=a[ from].size (); the for(i=0; i<l;i++) the { theto=a[ from][i]; the if(!Vis[to]) - { thedis[to]=dis[ from]+1; thevis[to]=true; the Que.push (to);94 } the } the } the }98 intClac_ans () About { - intx,y,z,sum=0;101 for(intI=1; i<=m;i++)102 {103x=b[i].u;104y=b[i].v; thez=B[i].ans;106sum=sum+ (Dis[x]+dis[y])-(dis[z]<<1);107 }108 returnsum;109 } the voidWork ()111 { the BFS ();113memset (Vis,false,sizeof(Vis)); theLca1,1); theprintf"%d\n", Clac_ans ()); the }117 intMain ()118 {119 read (); - Work ();121 return 0;122}
codevs1036
codevs1036 Business Trip