How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5448 Accepted Submission(s): 2065
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
AC代码如下:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{ int v,dis;
};
vector<node> G[41010];
int parent[35][41010],depth[41010],vis[41010],dis[41010],n,m,T,t;
void dfs(int v,int p,int d)
{ parent[0][v]=p;
depth[v]=d;
vis[v]=t;
int i,len=G[v].size();
for(i=0;i<len;i++)
if(vis[G[v][i].v]!=t)
{ dis[G[v][i].v]=dis[v]+G[v][i].dis;
dfs(G[v][i].v,v,d+1);
}
}
void init(int V)
{ int v,k;
dfs(1,-1,0);
for(k=0;k<30;k++)
for(v=1;v<=V;v++)
if(parent[k][v]<0)
parent[k+1][v]=-1;
else
parent[k+1][v]=parent[k][parent[k][v]];
}
int lca(int u,int v)
{ int k;
if(depth[u]>depth[v])
swap(u,v);
for(k=0;k<30;k++)
if((depth[v]-depth[u])>>k &1)
v=parent[k][v];
if(u==v)
return u;
for(k=29;k>=0;k--)
if(parent[k][u]!=parent[k][v])
{ u=parent[k][u];
v=parent[k][v];
}
return parent[0][u];
}
int main()
{ int u,v,i,j,k,d;
scanf("%d",&T);
node A;
for(t=1;t<=T;t++)
{ scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
G[i].clear();
for(i=1;i<n;i++)
{ scanf("%d%d%d",&u,&v,&d);
A.dis=d;
A.v=v;
G[u].push_back(A);
A.v=u;
G[v].push_back(A);
}
init(n);
while(m--)
{ scanf("%d%d",&u,&v);
d=lca(u,v);
printf("%d\n",dis[u]+dis[v]-2*dis[d]);
}
//printf("\n");
}
}