E - How far away ?(lca)

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

题目大意:给出一棵树,每条边上有边权,求x[i] x[i]x[i]到y[i] y[i]y[i]的路径边权之和。

思路概括:

LCA模板题。
任意两点x和y的距离为x到根节点的距离+ y到根的距离 - 2 * lca(x, y)到根的距离

在这里插入图片描述

例如:如果我们要求点4和点7的路径之和,我们可以先找到他们LCA点2 ,然后就有

dis[4][7] = dis[1][4] + dis[1][7] - 2 * dis[1][2];

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
#define lowbit(x) x&(-x)
#define lson l,mid,lt
#define rson mid+1,r,rt
using namespace std;
typedef long long  ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mem(a, b) memset(a, b, sizeof(a))
#define int ll
const double pi = acos(-1.0);
const double eps = 1e-6;
const double C = 0.57721566490153286060651209;
const ll mod = 1ll << 32;
const int inf = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const uint INF = 0xffffffff;
const int maxn = 4e4 + 5;
struct node
{
    int u, v, w, next;
    node (){};
    node(int u_, int v_, int w_, int next_)
    {
        u = u_;
        v = v_;
        w = w_;
        next = next_;
    }
}edge[maxn * 2];
struct nodd
{
    int u, v, id, next;
    nodd(){};
    nodd(int u_, int v_, int id_, int next_)
    {
        u = u_;
        v = v_;
        id = id_;
        next = next_;
    }
}qu[maxn * 2];
int n, m, ans;
int head[maxn], head1[maxn];
int cnt, cnt1;
int res[maxn][4];
int de[maxn], pre[maxn], fa[maxn];
bool vis[maxn];
void init()
{
    cnt = cnt1= 0;
    mem(vis, false);
    mem(de, 0);
    mem(head, -1);
    mem(head1, -1);
    mem(qu, 0);
    mem(edge, 0);
    for(int i=1;i<=n;i++)
    {
        fa[i] = i;
    }
}
void add_edge(int u, int v, int w)
{
    edge[cnt] = (node){u, v, w, head[u]};
    head[u] = cnt++;
}
void qu_edge(int u, int v, int id)
{
    qu[cnt1] = (nodd){u, v, id, head1[u]};
    head1[u] = cnt1++;
}
int find_fa(int x)
{
    if(fa[x] == x) return x;
    else return fa[x] = find_fa(fa[x]);
}
void Tarjan(int x)
{
    vis[x] = true;
    for(int i=head[x];~i;i=edge[i].next)
    {
        int v = edge[i].v;
        if(!vis[v])
        {
            de[v] = de[x] + edge[i].w;
            Tarjan(v);
            int r1 = find_fa(x);
            int r2 = find_fa(v);
            if(r1 != r2)
            {
                fa[r2] = r1;
            }
        }
    }
    for(int i=head1[x]; ~i; i=qu[i].next)
    {
        int v = qu[i].v;
        if(vis[v])
        {
            res[qu[i].id][2] = find_fa(v);
        }
    }
}
signed main()
{
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n >> m;
        init();
        for(int i=1;i<n;i++)
        {
            int u, v, w;
            cin >> u >> v >> w;
            add_edge(u, v, w);
            add_edge(v, u, w);
        }
        for(int i=1;i<=m;i++)
        {
            int u, v;
            cin >> u >> v;
            qu_edge(u, v, i);
            qu_edge(v, u, i);
            res[i][0] = u;
            res[i][1] = v;
        }
        Tarjan(1);
        for(int i=1;i<=m;i++)
        {
            cout << de[res[i][0]] + de[res[i][1]] - 2 * de[res[i][2]] << endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值