POJ 1986 Distance Queries(LCA在线和离线)

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.

离线


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
const int N=1e6+10;
int n,k,m;
int a[N],b[N];
struct node
{
    int u,v,w,next,lca;
} edge[N*2],edge1[N*2];
int head[N],head1[N],ip,ip1;
int Father[N],vis[N],dir[N],ans[N];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(dir,0,sizeof(dir));
    memset(head1,-1,sizeof(head1));
    memset(head,-1,sizeof(head));
    memset(Father,0,sizeof(Father));
    ip=ip1=0;
}
void addedge(int u,int v,int w)
{
    edge[ip].u=u, edge[ip].v=v, edge[ip].w=w, edge[ip].next=head[u],head[u]=ip++;
}
void addedge1(int u,int v)
{
    edge1[ip1].u=u, edge1[ip1].v=v, edge1[ip1].next=head1[u],head1[u]=ip1++;
}
int Find(int x)
{
    if(Father[x]==x) return x;
    else return Father[x]=Find(Father[x]);
}
void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if(x!=y)
        Father[y]=x;
}
void tarjan(int u)
{
    vis[u]=1;
    Father[u]=u;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(vis[v]) continue;
        dir[v]=dir[u]+w;
        tarjan(v);
        Union(u,v);
    }
    for(int i=head1[u]; i!=-1; i=edge1[i].next)
    {
        int v=edge1[i].v;
        if(vis[v])
        {
            edge1[i].lca=edge1[i^1].lca=Father[Find(v)];
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0; i<m; i++) // 建  图
        {
            int u,v,w;
            char str[10];
            scanf("%d%d%d%s",&u,&v,&w,str);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        scanf("%d",&k);
        for(int i=0; i<k; i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            addedge1(a[i],b[i]);
            addedge1(b[i],a[i]);
        }
        dir[1]=0;
        tarjan(1);
        for(int i=0; i<k*2; i+=2)
        {
            int d=dir[a[i/2]]+dir[b[i/2]]-2*dir[edge1[i].lca];
            printf("%d\n",d);
        }
    }
    return 0;
}

在线

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 40010
#define M 25

int tot;
int __pow[M];
int head[N];
struct edge{
    int u,v,w,next;
}e[2*N];
int ver[2*N],R[2*N],first[N],dir[N];
int dp[2*N][25];
bool vis[N];

inline void add(int u , int v ,int w ,int &k)
{
    e[k].u = u; e[k].v = v; e[k].w = w;
    e[k].next = head[u]; head[u] = k++;
    u = u^v; v = u^v; u = u^v;
    e[k].u = u; e[k].v = v; e[k].w = w;
    e[k].next = head[u]; head[u] = k++;
}

void dfs(int u ,int dep)
{
    vis[u] = true; first[u] = ++tot; ver[tot] = u; R[tot] = dep;
    for(int k=head[u]; k!=-1; k=e[k].next)
        if( !vis[e[k].v] )
        {
            int v = e[k].v , w = e[k].w;
            dir[v] = dir[u] + w;
            dfs(v,dep+1);
            ver[++tot] = u; R[tot] = dep;
        }
}

void ST(int len)
{
    int K = (int)(log((double)(len)) / log(2.0));
    for(int i=1; i<=len; i++) dp[i][0] = i;
    for(int j=1; j<=K; j++)
        for(int i=1; i+__pow[j]-1 <= len; i++)
        {
            int a = dp[i][j-1] , b = dp[i+__pow[j-1]][j-1];
            if(R[a] < R[b]) dp[i][j] = a;
            else            dp[i][j] = b;
        }
}

int RMQ(int x ,int y)
{
    int K = (int)(log((double)(y-x+1)) / log(2.0));
    int a = dp[x][K] , b = dp[y-__pow[K]+1][K];
    if(R[a] < R[b]) return a;
    else            return b;
}

int LCA(int u ,int v)
{
    int x = first[u] , y = first[v];
    if(x > y) swap(x,y);
    int index = RMQ(x,y);
    return ver[index];
}

int main()
{
    for(int i=0; i<M; i++) __pow[i] = (1<<i);
    int n,m,k,str[10];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        k = 0;
        memset(head,-1,sizeof(head));
        memset(vis,false,sizeof(vis));
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d%s",&u,&v,&w,str);
            add(u,v,w,k);
        }
        tot = dir[1] = 0;
        dfs(1,1);
        ST(tot);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int u,v,lca;
            scanf("%d%d",&u,&v);
            lca = LCA(u,v);
            printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值