How far away ?--LCA

How far away ?

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


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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586


最近刚学LCA,对于Tarjan的离线算法掌握的自认为差不多,然后去做了这个题,然后发现,不会做。。。然后去看了题解,一看就懂了,然后自己敲了一遍,最后又忘记*2了,脑子智障了,找了将近半小时,一个小时多点敲出来的这个题,有点慢了,不能自傲啊,果然一自傲和自满我就死了,要注意了。


题意大概是说有n个点,m次询问,每次询问让你求u,v之间的最小距离。


我们设res[][]数组记录询问,res[i][0]代表第i次询问起点u,res[i][1]代表第i次询问终点v,res[i][2]代表u,v的最近公共祖先,我们再开一个dis[]数组,代表此节点到根节点的距离,这样u,v的最近距离就是dis[res[i][0]]+dis[res[i][1]]-2*dis[res[i][2]];答案就出来了


LCA的求法具体见我上一篇博客

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=500000;
struct node{
    int w,v;
    struct node *pre;
}edge1[N],edge2[N],*p1[N],*p2[N];
int nEdge1,nEdge2;
int res[N][5];
bool vis[N];
int pre[N];
int dis[N];
void connect(int u,int v,int w,node edge[],node *p[],int &nEdge){
    nEdge++;
    edge[nEdge].v=v;
    edge[nEdge].w=w;
    edge[nEdge].pre=p[u];
    p[u]=edge+nEdge++;

    nEdge++;
    edge[nEdge].v=u;
    edge[nEdge].w=w;
    edge[nEdge].pre=p[v];
    p[v]=edge+nEdge++;
}
void Init(int n){
    memset(p1,0,sizeof(p1));
    memset(p2,0,sizeof(p2));
    memset(vis,false,sizeof(vis));
    memset(dis,0,sizeof(dis));
    memset(res,0,sizeof(res));
    nEdge1=0;
    nEdge2=0;
}
int Find(int x){
    if(x!=pre[x])
        return pre[x]=Find(pre[x]);
    return x;
}
void Tarjan(int x){
    vis[x]=true;
    pre[x]=x;
    for(node *p=p2[x];p;p=p->pre){
        if(vis[p->v]){
            res[p->w][2]=Find(p->v);
        }
    }
    for(node *p=p1[x];p;p=p->pre){
        if(!vis[p->v]){
            dis[p->v]=dis[x]+p->w;
            Tarjan(p->v);
            pre[p->v]=x;
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        Init(n);
        for(int i=0;i<n-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            connect(u,v,w,edge1,p1,nEdge1);
        }
        for(int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            res[i][0]=u;
            res[i][1]=v;
            connect(u,v,i,edge2,p2,nEdge2);
        }
        Tarjan(1);
        for(int i=0;i<m;i++){
            printf("%d\n",dis[res[i][0]]+dis[res[i][1]]-2*dis[res[i][2]]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值