How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34457 Accepted Submission(s): 13982
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
题意:给出房屋的个数 n ,边数为 n - 1,询问 m 次,每次询问两房屋之间的路径长度
思路:数据比较大,用 tarjin 离线求 lca,过程中维护距离值,dis[i]为点 i 到根节点1的距离
tarjin求lca
讲解:https://www.cnblogs.com/JVxie/p/4854719.html
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 4e4 + 10;
int t, n, m, u, v, w, head[N], head2[N], tot, tot2, fa[N], dis[N];
bool vis[N];
struct node
{
int v, next, dis;
}edge[N << 1];
struct query
{
int lca, u, v, next;
}q[205 << 1];
void init()
{
tot = 0;
tot2 = 0;
memset(head, -1, sizeof(head));
memset(head2, -1, sizeof(head2));
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
}
void add(int u, int v, int w)
{
edge[tot].next = head[u];
edge[tot].v = v;
edge[tot].dis = w;
head[u] = tot++;
}
void add2(int u, int v)
{
q[tot2].next = head2[u];
q[tot2].u = u;
q[tot2].v = v;
head2[u] = tot2++;
}
int Find(int x)
{
if(fa[x] != x)
fa[x] = Find(fa[x]);
return fa[x];
}
void tarjin(int u)
{
fa[u] = u;
vis[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].v, w = edge[i].dis;
if(!vis[v])
{
dis[v] = dis[u] + w;
tarjin(v);
fa[v] = u;
}
}
for(int i = head2[u]; ~i; i = q[i].next)
{
int v = q[i].v;
if(vis[v])
{
q[i].lca = q[i ^ 1].lca = Find(v);
}
}
}
int main()
{
scanf("%d", &t);
while(t--)
{
init();
scanf("%d%d", &n, &m);
for(int i = 1; i < n; ++i)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
for(int i = 1; i <= m; ++i)
{
scanf("%d%d", &u, &v);
add2(u, v);
add2(v, u);
}
tarjin(1);
for(int i = 0; i < m; ++i)
{
int u = q[2 * i].u;
int v = q[2 * i].v;
int w = q[2 * i].lca;
cout<<dis[u] + dis[v] - 2 * dis[w]<<'\n';
}
}
return 0;
}
树上倍增
树上倍增讲解和用法:https://blog.csdn.net/saramanda/article/details/54963914
树上倍增求lca:https://www.cnblogs.com/lbssxz/p/11114819.html
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 4e4 + 10;
//dis[i]:点i到根节点的距离
//fa[i][j]:节点i的第2^j个父亲,每个点最多2^(logN)个父亲,所以第二维开logN
//dep[i]:当前节点的深度
int n, m, dis[N], head[N], fa[N][21], tot, dep[N];
struct Edge
{
int to, next, w;
}edge[N << 1];
void init()
{
tot = 0;
memset(dis, 0, sizeof(dis));
memset(head, -1, sizeof(head));
memset(fa, 0, sizeof(fa));
}
void add(int u, int v, int w)
{
edge[tot].next = head[u];
edge[tot].to = v;
edge[tot].w = w;
head[u] = tot++;
}
void dfs(int u, int father) //当前节点和它的父亲
{
dep[u] = dep[father] + 1;
for(int i = 1; (1 << i) <= dep[u]; ++i)
{
if(fa[u][i - 1])
fa[u][i] = fa[fa[u][i - 1]][i - 1];
else break; //如果该点没有第2^(i - 1)个父亲了,也不会有更远的父亲
}
for(int i = head[u]; ~i; i = edge[i].next) //遍历相邻的所有点
{
int v = edge[i].to;
if(v != father) // v 不是 u 的父亲,就是 u 的儿子
{
dis[v] = dis[u] + edge[i].w; //更新距离
fa[v][0] = u; // v 的第2^0个父亲即第一个父亲是 u
dfs(v, u);
}
}
}
int lca(int u, int v)
{
if(dep[u] < dep[v]) //默认 u 比 v 深
swap(u, v);
for(int i = 20; i >= 0; --i) //从大到小枚举使 x 和 y 到达同一层
{
if(dep[fa[u][i]] >= dep[v])
u = fa[u][i];
if(u == v)
return u;
}
for(int i = 20; i >= 0; --i)
{
if(fa[u][i] != fa[v][i])
{
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int main()
{
int t, u, v, w;
scanf("%d", &t);
while(t--)
{
init();
scanf("%d%d", &n, &m);
for(int i = 1; i < n; ++i)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
dfs(1, 0); //选取1为根节点,1的父亲是0
while(m--)
{
scanf("%d%d", &u, &v);
cout<<dis[u] + dis[v] - 2 * dis[lca(u, v)]<<'\n';
}
}
return 0;
}