U - Distance Queries POJ - 1986 (LCA)

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.

求两个农场之间的距离

用tarjan 离线 LCA求公共祖先 然后两点的深度之和减去公共祖先的深度*2就是两点之间的距离。

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
const int N=1e+5;
const int prime=2333317;
const int INF=0x3f3f3f3f3f;
int n,m,k,k2,ans;
int pre[N],head[N],head2[N],dis[N],vis[N],is_root[N],ais[N];
struct node
{
    int v,next,w;
}edge[N*10];
struct Node
{
    int v,next,w;
}se[N*10];
struct we
{
    int x,y;
}qwer[N*10];
void init()
{
    memset(head2,-1,sizeof(head2));
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
    memset(is_root,0,sizeof(is_root));
    memset(vis,0,sizeof(vis));
    memset(ais,0,sizeof(ais));
    k=k2=ans=0;
    for(int i=1;i<=n;i++)
    {
        pre[i]=i;
    }
}
void add(int u,int v,int w)
{
    edge[k].v=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k++;
}
void add2(int u,int v,int w)
{
    se[k2].v=v;
    se[k2].w=w;
    se[k2].next=head2[u];
    head2[u]=k2++;
}
int Find(int x)
{
    if(x!=pre[x])
    {
        pre[x]=Find(pre[x]);
    }
    return pre[x];
}
void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
        pre[fy]=fx;
}
void dfs(int u)
{
    vis[u]=1;
    pre[u]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            dis[v]=dis[u]+edge[i].w;
            dfs(v);
            vis[v]=1;
            join(u,v);

        }
    }
    for(int i=head2[u];i!=-1;i=se[i].next)
    {
        int v=se[i].v;
        int w=se[i].w;
        if(vis[v])
        {
             ais[w]=Find(v);
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int x,y,z;
        char s;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d %c",&x,&y,&z,&s);
            is_root[y]=1;
            add(x,y,z);
            add(y,x,z);
        }
        int nu;
        scanf("%d",&nu);
        for(int i=1;i<=nu;i++)
        {
            scanf("%d%d",&x,&y);
            qwer[i].x=x;
            qwer[i].y=y;
            add2(x,y,i);
            add2(y,x,i);
        }
        int root;
        for(int i=1;i<=n;i++)
        {
            if(!is_root[i])
                root=i;
        }
        dfs(root);
        for(int i=1;i<=nu;i++)
         {
             printf("%d\n",dis[qwer[i].x]+dis[qwer[i].y]-2*dis[ais[i]]);
         }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值