How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12063 Accepted Submission(s): 4445 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people. Input First line is a single integer T(T<=10), indicating the number of test cases. For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n. Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j. Output For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case. Sample Input 2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1 Sample Output 10 25 100 100
LCA在线ST:对一颗有根树进行DFS搜索,无论递归还是回溯,每次到达一个节点都将节点的编号记录下来,这样就得到了一条长度为2*n-1的欧拉序列,这样在序列中,从u到v
一定会有u,v的祖先,而不会有u,v祖先节点的祖先,而且u,v之间深度最小的节点就是LCA(u,v),再使用ST算法求RMQ,这样每次查询的时间就能达到O(1)
#include <iostream> #include <cstdio> #include <cstring> #define scan(x) scanf("%d",&x) #define scan2(x,y) scanf("%d%d",&x,&y) #define scan3(x,y,z) scanf("%d%d%d",&x,&y,&z) using namespace std; const int Max=40010; const int E=40010*2; int head[Max],nex[E],pnt[E],cost[E],edge; int vex[Max<<1],R[Max<<1],vis[Max],dis[Max],first[Max],tot; //!!vex R 长度要Max*2,因为算法特性会生成顶点数两倍的序列 int n; void Addedge(int u,int v,int c) { pnt[edge]=v;cost[edge]=c; nex[edge]=head[u];head[u]=edge++; } void dfs(int u,int deep) { vis[u]=1; vex[++tot]=u; //以tot为编号的的节点 first[u]=tot; //u节点的编号为tot R[tot]=deep; //tot编号节点的深度 for(int x=head[u];x!=-1;x=nex[x]) { int v=pnt[x],c=cost[x]; if(!vis[v]) { dis[v]=dis[u]+c; dfs(v,deep+1); vex[++tot]=u; R[tot]=deep; } } } int dp[Max<<1][25]; //!!dp长度要Max*2,,因为算法特性会生成顶点数两倍的序列 void ST(int n) //n是2*n-1 { int x,y; for(int i=1;i<=n;i++) dp[i][0]=i; for(int j=1;(1<<j)<=n;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { x=dp[i][j-1];y=dp[i+(1<<(j-1))][j-1]; dp[i][j]=(R[x]<R[y]?x:y); } } } int RMQ(int l,int r) { int k=0,x,y; while((1<<(k+1))<=r-l+1) k++; x=dp[l][k];y=dp[r-(1<<k)+1][k]; return (R[x]<R[y])?x:y; } int LCA(int u,int v) { int x=first[u],y=first[v]; if(x>y) swap(x,y); int res=RMQ(x,y); //在u,v之间的最小深度节点即为lca return vex[res]; } void Init() { edge=0; memset(head,-1,sizeof(head)); memset(nex,-1,sizeof(nex)); memset(vis,0,sizeof(vis)); } int main() { int T,Q; for(scan(T);T;T--) { Init(); int u,v,c; scan2(n,Q); for(int i=0;i<n-1;i++) { scan3(u,v,c); Addedge(u,v,c); Addedge(v,u,c); } tot=0;dis[1]=0; dfs(1,1); ST(2*n); while(Q--) { scan2(u,v); int lca=LCA(u,v); printf("%d ",dis[u]+dis[v]-2*dis[lca]); } } return 0; }