How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5082 Accepted Submission(s): 1921
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.
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
思路:用E数组dfs走过的所有点,除了叶子节点其他的都会记录多次,然后R【i】记录的是编号是i的节点在数组E出现的位置,dis记录记录深度,RMQ中的dp通过dis记录深度最小的节点编号
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<utility>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define Inf (1<<30)
#define LL long long
#define MOD 1000000009
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MM=80010;
int n,m,num;
int head[MM],cnt;
int dis[MM];
int E[MM],R[MM];
struct edg{
int w,u,v,next;
}edge[MM];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u,int d,int last)
{
if(R[u]==-1)R[u]=num;
E[num++]=u;
dis[u]=d;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(v==last)continue;
dfs(v,d+edge[i].w,u);
E[num++]=u;
}
}
int dp[MM][17];
void RMQ_init()
{
int L=num;
for(int i=0;i<L;i++)dp[i][0]=E[i];
for(int j=1;(1<<j)<=num;j++)
{
for(int i=0;(i+(1<<j))<=num;i++)
if(dis[dp[i][j-1]]<dis[dp[i+(1<<(j-1))][j-1]])dp[i][j]=dp[i][j-1];
else dp[i][j]=dp[i+(1<<(j-1))][j-1];
}
}
int RMQ(int L,int R)
{
if(L>R)swap(L,R);
int k=log(R-L+1.0)/log(2.0);
return min(dp[L][k],dp[R-(1<<k)+1][k]);
}
int LCA(int u,int v)
{
return RMQ(R[u],R[v]);
}
int main() {
int T;
scanf("%d",&T);
while(T--)
{
cnt=0;
scanf("%d%d",&n,&m);
memset(R,-1,sizeof(R));
memset(head,-1,sizeof(head));
for(int i=0;i<n-1;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
num=0;
dfs(1,0,0);
RMQ_init();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
int l=LCA(u,v);
printf("%d\n",dis[u]+dis[v]-2*dis[l]);
}
}
return 0;
}