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.
outputs description output
Description
outputs the shortest time the merchant travels in the output file.
Sample input
5
1 2
1 5
3 5
4 5
4
1
3
2
5
Sample output
7
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include < algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include < map>using namespace Std;const int maxn = 30000 + 10;int rmq[2*maxn];//build RMQ array//***************************//st algorithm, It contains initialization init (n) and query (s,t) function//point numbering starting from 1, 1-n. Returns the lowest value subscript//***************************struct st{int mm[2*maxn];//mm[i] Represents the highest bit of I, mm[1]=0,mm[2]=1,mm[3]=1,mm[4]=2 int dp[maxn*2][20]; void init (int n) {mm[0]=-1; for (int i=1;i<=n;i++) {mm[i]= ((i& (i-1)) ==0?mm[i-1]+1:mm[i-1]); Dp[i][0]=i; } for (int j=1;j<=mm[n];j++) for (int i=1;i+ (1<<J) -1<=n;i++) dp[i][j]=rmq[dp[i][j-1] ]<rmq[dp[i+ (1<< (j-1))][j-1]]?dp[i][j-1]:d p[i+ (1<< (j-1))][j-1]; } int query (int a,int b)//Query the subscript {if (a>b) swap (b) for the minimum value between A and B; int k=mm[b-a+1]; Return rmq[dp[a][k]]<rmq[dp[b-(1<<k) +1][k]]?dp[a][k]:d p[b-(1<<k) +1][k]; struct node{int to,next;};/with}};//edges defined * ******************************************LCA conversion to RMQ problem MAXN is the maximum node count. The array of St and the F,edge to be set to 2*MAXNF are Euler sequences, RMQ is the depth sequence, and P is the subscript for the first occurrence of a point in F *********************************************/struct lca2rmq{int n;//Node number node edge[2*maxn];//The edge of the tree because it is built without an edge, so it is twice times the int tol;//side of the count int head[maxn];//head node bool Vis[ma The xn];//access token int f[2*maxn];//f is a Euler sequence, which is the order of the DFS traversal int p[maxn];//a point in F where the first occurrence of the position int cnt; St St; void init (int n)//n is the total number of points, which can start at 0 or start with 1 {this->n=n; tol=0; memset (head,-1,sizeof (head)); } void Addedge (int a,int b)//plus side {edge[tol].to=b; Edge[tol].next=head[a]; head[a]=tol++; Edge[tol].to=a; EDGE[TOL].NEXT=HEAD[B]; head[b]=tol++; } int query (int a,int b)//pass in two nodes and return their LCA number {return f[st.query (P[a],p[b])]; } void Dfs (int a,int lev) {vis[a]=true;++cnt;//first, to ensure that the F sequence and RMQ sequence starting from 1 f[cnt]=a;//Euler sequence, numbering starting from 1, a total of 2*n-1 elements rmq[cnt]=lev;//rmq array is the depth sequence p[a]=cnt; for (int i=head[a];i!=-1;i=edge[i].next) {int v=edge[i].to; if (Vis[v]) continue; DFS (V,LEV+1); ++cnt; F[cnt]=a; Rmq[cnt]=lev; }} void solve (int root) {memset (vis,false,sizeof (VIS)); cnt=0; DFS (root,0); St.init (2*n-1); }}lca;int dis[maxn];vector<int>g[maxn];int p[maxn];void BFs () {queue<int>q; Q.push (1); memset (Dis,-1, sizeof (DIS)); Dis[1] = 0; while (! Q.empty ()) {int u = q.front (); Q.pop (); int sz = G[u].size (); for (int i=0;i<sz;i++) {int v = g[u][i]; cout << v << Endl; if (dis[v] = = 1) {Dis[v] = Dis[u] + 1; Q.push (v); }}}}int Main () {int N; while (scanf ("%d", &Amp N)!=eof) {int u, v; Lca.init (N); for (int i=0;i<=n;i++) g[i].clear (); for (int i=1;i<n;i++) {scanf ("%d%d", &u, &v); Lca.addedge (U, v); G[u].push_back (v); G[v].push_back (U); } BFS (); Lca.solve (1); int ans = 0; int M; scanf ("%d", &m); for (int i=1;i<=m;i++) scanf ("%d", &p[i]); for (int i=1;i<m;i++) {int fa = Lca.query (P[i], p[i+1]); cout << p[i] << ' << p[i+1] << ' << fa << Endl; Ans + = (Dis[p[i]] + dis[p[i+1]]-2 * DIS[FA]); } printf ("%d\n", ans); } return 0;}
CODE[VS] 1036 Business Travel (LCA + BFS)