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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章