poj 1986 Distance Queries(LCA模板题)

本文探讨了在树状结构中快速计算两点间距离的问题,通过离线算法优化求解过程,有效处理大规模查询需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Distance Queries
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 6773 Accepted: 2380
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare" 

* Line 2+M: A single integer, K. 1 <= K <= 10,000 

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

Source

题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。


分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。先在问题就是如何快速求LCA,由于结点数目比较大,查询比较多,所以用在线算法会超时。这里用的是tarjan离线算法,时间复杂度为O(n+k)。

贴上代码当模版:

#include <cstdio>
#include <cstring>
#define N 40010
#define M 80010
#define Q 20010
int n,m,q,e,eq;
int first[N],next[M],v[M],w[M];
int first_q[N],next_q[Q],u_q[Q],v_q[Q],lca[Q];
int d[N],p[N];
bool ok[Q];
void init()
{
    e=eq=0;
    memset(first,-1,sizeof(first));
    memset(first_q,-1,sizeof(first_q));
    memset(lca,0,sizeof(lca));
    memset(p,-1,sizeof(p));
    memset(ok,0,sizeof(ok));
}
void add(int a,int b,int c)
{
    v[e]=b;
    w[e]=c;
    next[e]=first[a];
    first[a]=e++;
}
void add_q(int a,int b)
{
    u_q[eq]=a;
    v_q[eq]=b;
    next_q[eq]=first_q[a];
    first_q[a]=eq++;
}


void make_set(int i)
{
    p[i]=i;
}
int find_set(int i)
{
    if(i^p[i])  p[i]=find_set(p[i]);
    return p[i];
}
void union_set(int i,int j)
{
    i=find_set(i),j=find_set(j);
    p[j]=i;
}
void get_d(int a,int fa)
{
    int i,b;
    for(i=first[a];i!=-1;i=next[i])
    {
        b=v[i];
        if(b^fa)    d[b]=d[a]+w[i],get_d(b,a);
    }
}
void dfs(int a)
{
    int i,b;
    make_set(a);
    for(i=first[a];i!=-1;i=next[i])
    {
        b=v[i];
        if(p[b]==-1)
        {
            dfs(b);
            union_set(a,b);
        }
    }
    for(i=first_q[a];i!=-1;i=next_q[i]) if(!ok[i])
    {
        b=v_q[i];
        if(p[b]!=-1)    lca[i]=find_set(b),ok[i]=true;
    }
}
int main()
{
    int a,b,c;
    char s[3];
    while(~scanf("%d%d",&n,&m))
    {
        init();
        while(m--)
        {
            scanf("%d%d%d%s",&a,&b,&c,s);
            add(a,b,c);
            add(b,a,c);
        }
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&a,&b);
            add_q(a,b);
            add_q(b,a);
        }
        d[1]=0;
        get_d(1,0);
        dfs(1);
        for(int i=0;i<eq;i+=2)
        {
            a=u_q[i];
            b=v_q[i];
            c=lca[i];
            if(!c)  c=lca[i+1];
            printf("%d\n",d[a]+d[b]-2*d[c]);
        }
    }
    return 0;
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值