# HDOJ2586 How far away ---- Tarjan算法离线查询最近公共祖先（LCA）

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

题意：

t组测试数据。  每组数据先给定n,m表示n个点m次询问。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 44000
using namespace std;
int n,m;
bool visited[MAXN]; // 标记某个节点是否访问过
bool handled[MAXN]; // 标记tarjan
int dis[MAXN];  // 到根节点距离
int pre[MAXN];  // 并查集中的父节点
int result[MAXN];
struct Node {
int to,weight;
};
vector<Node> graph[MAXN];
vector<int> query[MAXN],number[MAXN]; //询问，询问的编号
// 路径压缩查找祖先节点
int Find(int x) {
if(pre[x] == x) return x;
return pre[x] = Find(pre[x]);
}

// 初始化
void Init() {
memset(dis,0,sizeof(dis));
memset(visited,0,sizeof(visited));
memset(handled,0,sizeof(handled));
for(int i = 0;i <= n;i++) {
graph[i].clear(); // 清空图
query[i].clear();
number[i].clear();
pre[i] = i;
}
}

void Tarjan(int root,int len) {
dis[root] = len;
visited[root] = true;
int size = graph[root].size();  // 邻接节点数
for(int i = 0;i < size;i++) {
if(visited[graph[root][i].to]) continue;
Tarjan(graph[root][i].to, len+graph[root][i].weight);
pre[graph[root][i].to] = root; // 合并节点
}
// 处理与根节点相关的询问
size = query[root].size();
for(int i = 0;i < size;i++) {
if(handled[number[root][i]])
result[number[root][i]] = dis[root] + dis[query[root][i]] - 2*dis[Find(query[root][i])];
handled[number[root][i]] = true;
}
}

int main()
{
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
Init();
int a,b,c;
// 输入边
for(int i = 1;i < n;i++) {
scanf("%d%d%d",&a,&b,&c);
graph[a].push_back((Node){b,c});
graph[b].push_back((Node){a,c});
}
// m次询问
for(int i = 0;i < m;i++) {
scanf("%d%d",&a,&b);
query[a].push_back(b);
number[a].push_back(i);
query[b].push_back(a);
number[b].push_back(i);
}

Tarjan(1,0);
for(int i = 0;i < m;i++)
printf("%d\n",result[i]);

}

return 0;
} 

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120