How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18132 Accepted Submission(s): 7038
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
Source
这个版本的LCA就是把树的节点的第一次访问的时间戳,深度,时间戳保存下来,然后用RMQ查询两点之间的深度,两点之间深度最小的一定就是公共祖先了。
然后nlogn预处理时间戳、深度的RMQ 然后O1的查询。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
const int maxn = 40505;
int dfs_clock; //到根节点的距离 点的第一次clock clock的深度 第clock是哪个节点
int dp[maxn << 1][30], visited[maxn], dist[maxn], pre[maxn << 1], deep[maxn << 1], ver[maxn << 1];
struct Node {
int u, v, w, next;
}node[maxn << 1];
int head[maxn], num;
void add(int u, int v, int w) {
node[num] = {u, v, w, head[u]};
head[u] = num++;
node[num] = {v, u, w, head[v]};
head[v] = num++;
}
void init() {
memset(head, -1, sizeof(head));
memset(dp, 0, sizeof(dp));
memset(dist, 0, sizeof(dist));
memset(visited, 0, sizeof(visited));
num = 0;
dfs_clock =1;
}
void dfs(int u, int d) {
visited[u] = 1, pre[u] = dfs_clock, deep[dfs_clock] = d, ver[dfs_clock++] = u;
for (int i = head[u]; i != -1; i = node[i].next) {
int v = node[i].v;
int w = node[i].w;
if (!visited[v]) {
dist[v] = dist[u] + w;
dfs(v, d + 1);
ver[dfs_clock] = u;
deep[dfs_clock++] = d;
}
}
}
void ST(int n) {
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) {
int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
dp[i][j] = deep[a] < deep[b] ? a : b;
}
}
}
int RMQ(int left, int right) {
int k = 0;
while ((1 << (k + 1)) <= right - left + 1)
++k;
int a = dp[left][k], b = dp[right - (1 << k) + 1][k];
return deep[a] < deep[b] ? a : b;
}
int LCA(int x, int y) {
int a = pre[x], b = pre[y];
if (a > b)
swap(a, b);
return ver[RMQ(a, b)];
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
cin >> t;
while (t--) {
init();
int n, m;
cin >> n >> m;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
}
dfs(1, 1);
ST(2 * n - 1);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
cout << dist[x] + dist[y] - 2 * dist[LCA(x, y)] << endl;
}
}
}