1036 Business Travel
Time limit: 1 s
Space limit: 128000 KB
Title Level: Diamonds Diamond
Exercises
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 a road connection. 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.
Outputs description Output Description
Outputs the shortest time the merchant travels in the output file.
Sample input to sample
5
1 2
1 5
3 5
4 5
4
1
3
2
5
Sample output Sample Outputs
7
Data Size & Hint
No ring Qaq
Entire graph n points n-1 Edge
So this is a tree.
What is the relationship between two points on a tree?
Take a look at the picture ↓
The first point 2 and Point 4, point 3 and point 7 this on the same chain
The second point 4 and Point 5, point 5 and point 6 are separated by 108,000.
It's good to go straight to the first one
for the second kind of query common ancestor ~
with point 4 and point 5 for example
is 4 depth + 5 Depth-2 * 2 depth
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
const int MAXN = 70005;
int n,m,qq,tot = 0;
int F,t,x,ans = 0,now = 1;
int FIRST[MAXN],NEXT[MAXN];
int FA[MAXN][35],DEEP[MAXN]; struct Edge {int f,t;}
L[MAXN];
void Init () {memset (first,0xff,sizeof (first));
tot = 0;
Return
} void Build (int f,int t) {l[++ tot] = (edge) {f,t};
Next[tot] = first[f];
FIRST[F] = tot;
Return
} void Dfs (int x,int f) {fa[x][0] = f;
DEEP[X] = deep[f] + 1;
for (int i = first[x]; i =-1; i = Next[i]) if (l[i].t! = f) Dfs (L[I].T,X);
Return } void Make_fa () {for (int j = 1; J <=; J + +) for (int i = 1; I <= n; i + +) fa[i][j] = FA [Fa[i][j-1]]
[J-1];
Return
} int LCA (int x,int y) {if (Deep[x] < deep[y]) swap (x, y);
for (int j = k; J >= 0; J--) if (Deep[fa[x][j]] >= deep[y]) x = Fa[x][j]; if (x = = y) rEturn x;
for (int j = k; J >= 0;j--) if (fa[x][j]! = fa[y][j]) x = Fa[x][j],y = Fa[y][j];
return fa[x][0];
} int Ask (int x,int y) {return deep[x] + deep[y]-(DEEP[LCA (x, y)] << 1);}
int main () {init ();
scanf ("%d", &n);
for (int i = 1; i < n; i + +) {scanf ("%d%d", &f,&t);
Build (F,t), build (T,F);
} Deep[0] =-1;
DFS (1,0);
Make_fa ();
scanf ("%d", &qq);
while (QQ-) {scanf ("%d", &x);
Ans + = ask (Now,x);
now = x;
} printf ("%d\n", ans);
return 0; }