欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ1986:Distance Queries(LCA求距离 + Tarjan)

程序员文章站 2022-05-27 16:14:53
...

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 13490   Accepted: 4779
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

题意:给N个点,M条边(后面的字母可忽略),保证是一棵连通树,Q个询问,输出两个点的距离。

思路:用LCA计算两点距离,用Tarjan离线处理计算出所有答案,下面代码采用前向星存树。

# include <stdio.h>
# include <string.h>
# define maxn 100000
# define maxq 50000
int cnt, cnt2, pre[maxn], next[maxn], next2[maxn], dis[maxn], vis[maxn];
struct node
{
    int e, w, next;
}edge[maxn];

struct node2
{
    int anc, e, next;
}query[maxq];

void init(int n)
{
    cnt = cnt2 = 0;
    for(int i=0; i<=n; ++i)
        pre[i] = i;
    memset(next, -1, sizeof(next));
    memset(next2, -1, sizeof(next2));
    memset(edge, 0, sizeof(edge));
    memset(dis, 0, sizeof(dis));
    memset(query, 0, sizeof(query));
    memset(vis, 0, sizeof(vis));
}

int find(int x)
{
    if(x != pre[x])
        pre[x] = find(pre[x]);
    return pre[x];
}

void uni(int a, int b)//并查集。
{
    int x = find(a);
    int y = find(b);
    if(x != y)
        pre[y] = x;
}

void add_edge(int a, int b, int c)//存边。
{
    edge[cnt].e = b;
    edge[cnt].w = c;
    edge[cnt].next = next[a];
    next[a] = cnt++;
}

void add_query(int a, int b)//存询问。
{
    query[cnt2].e = b;
    query[cnt2].next = next2[a];
    next2[a] = cnt2++;
}

void lca(int u)//LCA。
{
    vis[u] = 1;
    for(int i=next[u]; i!=-1; i=edge[i].next)
    {
        int v = edge[i].e;
        if(vis[v]) continue;
        int w = edge[i].w;
        dis[v] = dis[u] + w;
        lca(v);
        uni(u, v);
    }
    for(int i=next2[u]; i!=-1; i=query[i].next)
    {
        int v = query[i].e;
        if(vis[v])
            query[i].anc = query[i^1].anc = find(v);
    }
}

int main()
{
    int n, m, q, a, b, c;
    char d;
    while(~scanf("%d%d",&n,&m))
    {
        init(n);
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d %c",&a,&b,&c,&d);
            add_edge(a, b, c);
            add_edge(b, a, c);
        }
        scanf("%d",&q);
        for(int i=0; i<q; ++i)
        {
            scanf("%d%d",&a,&b);
            add_query(a, b);
            add_query(b, a);
        }
        lca(1);
        for(int i=0; i<q; ++i)
        {
            int s = query[i<<1].e;
            int e = query[i<<1|1].e;
            int anc = query[i<<1].anc;
            printf("%d\n",dis[s]+dis[e]-(dis[anc]<<1));
        }
    }
}