HDU 2586 How far away ? (LCA_Tarjan)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23711    Accepted Submission(s): 9434


 

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.

 

 

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

ECJTU 2009 Spring Contest

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 2818 3038 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
const int  maxn =4e4+5;
const int mod=1e9+7;
/*
题目大意:一棵树,数个询问,
询问两个点的最短路径是多少。
裸的LCA.

这道题目练手LCA_Tarjan写法。
下面简单说说这个算法吧,
就是不断的利用递归性质构造出相异代表系,
每次构造划分完后就离线处理询问,
因为如果询问的点在构造的堆中的话,那么
这个时候的根节点就是LCA,
随着LCA不断的推进,把答案填充完毕。


*/
///并查集
int fa[maxn];
int Find(int x){return (x==fa[x])?x:fa[x]=Find(fa[x]);}
void Union(int x,int y)
{
    int fx=Find(x),fy=Find(y);
    fa[fy]=fx;
}
///链式前向星
struct node{int u,nxt,w;};
node edge[maxn<<1];
int head[maxn],tot=0;
void add(int x,int y,int w){ edge[tot]=node{y,head[x],w}; head[x]=tot++; }
bool vis[maxn];///是否访问过
int ans[maxn];///离线的答案数组
int dis[maxn];///子节点到根节点的距离数组
vector<int> v[maxn],num[maxn];///离线的集合,num为答案集合
void init()
{
    for(int i=0;i<maxn;i++)
    {
        fa[i]=i;
        vis[i]=false;
        head[i]=-1;

        dis[i]=0;
        ans[i]=0;

        v[i].clear();
        num[i].clear();
    }
}

void tarjan(int u,int w)
{
    vis[u]=1;
    dis[u]=w;
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].u;
        if(vis[v]) continue;
        tarjan(v,edge[i].w+w);
        Union(u,v);
    }

    for(int i=0;i<v[u].size();i++)
    {
        int p=v[u][i];
        if(vis[p] == 0) continue;
        ans[num[u][i]]=dis[u]+dis[p]-2*dis[Find(p)];
        ///cout<<u<<" "<<dis[u]<<" "<<p<<" "<<dis[p]<<endl;
        ///cout<<p<<" "<<Find(p)<<" "<<dis[Find(p)]<<endl;
    }
}
int n,m,x,y,z;

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
            num[x].push_back(i);
            num[y].push_back(i);
        }
        tarjan(1,0);
        for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值