How far away ? HDU - 2586
Title Link
Give a tree the minimum distance between any two points;
Thinking: considering the problem, if t is the LCA of a and B, r is the root of tree, then dis[a, b]=dis[r, a]+dis[r, b]-2*dis[r, t];
dis[r, p] (the distance from root to point p) can be obtained by dfs, so finding LCA of [a, b] is all right;
First, Tarjan offline method:
1. Select any root node;
2. Traverse all the sub nodes v of the current node u and mark them;
3. If there is a child node at the current point, return to 2, otherwise enter the next step;
4. Use union to look up the set and merge v into u
5. Traverse the point v related to the current point u in all queries. If x is marked (accessed), then lca(u, v)=find(v);
The complexity is O(n+q);
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=4e4+10;
struct node{
int v, nxt, w;
}edge[maxn<<1];
int head[maxn], cnt;
void add(int u, int v, int w){
edge[cnt]=node{v, head[u], w};
head[u]=cnt++;
}
int n, m;
int per[maxn], vis[maxn], dis[maxn];
vector<int> q[maxn];
map<pair<int, int>, int> mp;
void init(){
cnt=0;
mp.clear();
for(int i=0; i<=n; i++){
per[i]=i;
head[i]=-1;
vis[i]=0;
q[i].clear();
}
}
int find(int x){
return per[x]==x?per[x]:per[x]=find(per[x]);
}
void Union(int u, int v){
int fu=find(u), fv=find(v);
per[fv]=fu;
}
struct query{
int u, v;
}p[maxn];
void tarjan(int u, int fa){
for(int i=head[u]; i!=-1; i=edge[i].nxt){
int v=edge[i].v, w=edge[i].w;
if(v==fa) continue;
dis[v]=dis[u]+w;
tarjan(v, u);
Union(u, v);
vis[v]=1;
}
for(int i=0; i<q[u].size(); i++){
int v=q[u][i];
if(vis[v]){
int t=find(v);
int d=dis[u]+dis[v]-2*dis[t];
mp.insert(make_pair(make_pair(u, v), d));
}
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
init();
for(int i=1; i<n; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
for(int i=0; i<m; i++){
int u, v;
scanf("%d%d", &u, &v);
p[i]=query{u, v};
q[u].push_back(v);
q[v].push_back(u);
}
dis[1]=0;
tarjan(1, 1);
for(int i=0; i<m; i++){
if(mp.count(make_pair(p[i].u, p[i].v))){
printf("%d\n", mp[make_pair(p[i].u, p[i].v)]);
}
else if(mp.count(make_pair(p[i].v, p[i].u))){
printf("%d\n", mp[make_pair(p[i].v, p[i].u)]);
}
}
}
return 0;
}
Then there is the multiplication on-line method:
Solving steps:
1. Adjust u and V to the same depth;
2. Jump up together until u=v;
Of course, we can jump up step by step, but we can imagine that the complexity is O(n), then q queries, the whole is O(nq) complexity; obviously not appropriate; so long as we can jump a little more;
If jump[i][j] means to jump up 2^j steps at point I, then jump[i][j] = jump [J [i] [J-I]] [J-1];
#include <bits/stdc++.h>
using namespace std;
const int maxn=4e4+10;
struct node{
int v, nxt, w;
}edge[maxn<<1];
int head[maxn], cnt;
void add(int u, int v, int w){
edge[cnt]=node{v, head[u], w};
head[u]=cnt++;
}
int n, m, deep[maxn], jump[maxn][15], dis[maxn], k;
void dfs(int u, int fa, int d){
deep[u]=d;
for(int i=head[u]; i!=-1; i=edge[i].nxt){
int v=edge[i].v, w=edge[i].w;
if(v==fa) continue;
dis[v]=dis[u]+w;
jump[v][0]=u;
for(int i=1; (1<<i)<=n; i++){
jump[v][i]=jump[jump[v][i-1]][i-1];
if(k<i) k=i;
}
dfs(v, u, d+1);
}
}
void init(){
memset(jump, 0, sizeof(jump));
memset(deep, 0, sizeof(deep));
dis[1]=0;
dfs(1, 1, 0);
}
int LCA(int u, int v){
if(deep[u]<deep[v]) swap(u, v);
for(int j=k; j>=0; j--){
if(deep[u]-(1<<j)>=deep[v]){
u=jump[u][j];
}
}
if(u==v) return u;
for(int j=k; j>=0; j--){
if(jump[u][j]!=jump[v][j]){
u=jump[u][j];
v=jump[v][j];
}
}
return jump[u][0];
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
memset(head, -1, sizeof(head));
cnt=0;
for(int i=1; i<n; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
init();
while(m--){
int u, v;
scanf("%d%d", &u, &v);
int t=LCA(u, v);
printf("%d\n", dis[u]+dis[v]-2*dis[t]);
}
}
return 0;
}