hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。

VIE

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<stdlib.h>
using namespace std;
const int mmax= 40010;
const int mod=1000000007;
typedef long long LL;

struct node
{
    int en,len;
    int next;
}E[2*mmax];
int p[mmax],num;
void init()
{
    memset(p,-1,sizeof p);
    num=0;
}
void add(int st,int en,int len)
{
    E[num].en=en;
    E[num].len=len;
    E[num].next=p[st];
    p[st]=num++;
}
struct nodes
{
    int v;
    int next;
}Q[mmax];
int Qp[mmax];
int Qnum;
void initQ()
{
    memset(Qp,-1,sizeof Qp);
    Qnum=0;
}
void addQ(int u,int v )
{
    Q[Qnum].v=v;
    Q[Qnum].next=Qp[u];
    Qp[u]=Qnum++;
}
int dis[mmax],fa[mmax],ans[mmax];
bool vis[mmax];
int find(int x)
{
    if(x==fa[x])
        return x;
    return fa[x]=find(fa[x]);
}
void Lca(int u)
{
    vis[u]=1;
    for(int i=Qp[u];i+1;i=Q[i].next)
    {
        int v=Q[i].v;
        if(vis[v])
            ans[i/2]=dis[u]+dis[v]-2*dis[find(v)];
    }
    for(int i=p[u];i+1;i=E[i].next)
    {
        int v=E[i].en;
        if(!vis[v])
        {
            dis[v]=dis[u]+E[i].len;
            Lca(v);
            fa[v]=u;
        }
    }
}
int main()
{
    int t,n,m;
    int u,v,len;
    cin>>t;
    while(t--)
    {
        init();
        initQ();
        scanf("%d %d",&n,&m);
        for(int i=0;i<=n;i++)
            fa[i]=i;
        for(int i=1;i<n;i++)
        {
            scanf("%d %d %d",&u,&v,&len);
            add(u,v,len);
            add(v,u,len);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&u,&v);
            addQ(u,v);
            addQ(v,u);
        }
        memset(vis,0,sizeof vis);
        dis[1]=0;
        Lca(1);
        for(int i=0;i<m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

W CODE

时间: 2024-11-13 10:03:45

hdu 2586 树上点对最近距离 (lca)的相关文章

hdu 2586 How far away ?倍增LCA

hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码: #include <iostream> #include <algorithm> #includ

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): 6422    Accepted Submission(s): 2411 Problem Description There are n houses in the village and some bidirectional roads connecting

HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

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

HDU 2586 How far away ?(LCA在线算法实现)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的距离,然后只要计算出两点的LCA,这样一来答案就是distance[u]+distance[v]-2distance[LCA]. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #includ

HDU 2586 How far away ?(LCA模板 最近公共祖先啊)

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

HDU 2586 How far away ? 离线lca模板题

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8712    Accepted Submission(s): 3047 Problem Description There are n houses in the village and some bidirectional roads connecting

HDU 2586 How far away?(LCA使用详解)

关键词:LCA.并查集.动态规划.深度优先搜索.哈希.RMQ.递归 题目: 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 ha

HDU 2586 查找两点之间的距离

描述 村里有n个房子和一些双向道路连接. 每天这样的人总是喜欢问“如果我想有多远从房子房子B”? 通常很难回答. 但幸运的是这个村子int答案永远是独一无二的,因为道路建造的方式有一个独特的简单路径(“简单”意味着你不能访问一个地方两次)每两间房子. 你的任务是回答所有这些好奇的人们. 输入 第一行是一个整数T(T < = 10),说明测试用例的数量.对于每个测试用例,在第一行有两个数n(2 < = n < = 40000)和m(1 < = m < = 200),房屋的数量和

LCA(最近公共祖先)--tarjan离线算法 hdu 2586

HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description There are n houses in the village and some bidirectional roads c