hdoj 2586 How far away ? 【Tarjan离线LCA】

题目:hdoj 2586 How far away ?

题意:给出一个有权树,求任意两点的之间的距离。

分析:思想就是以一个点 root 作为跟变成有根数,然后深搜处理处所有点到跟的距离。求要求的两个点的LCA(最近公共祖先),

然后ans = dis【x】 + dis【y】 - 2 * dis【LCA(x,y)】,可以画图分析一下就知道。

求LCA我用的是Tarjan离线lca,由于询问次数很多,所以这个比较快。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
const int N = 44000;
#define INF 1000000000
using namespace std;
struct Node
{
    int to,val;
};
vector<Node> v[N];
vector<int> query[N],num[N];
int ans[N];
bool vis[N];
int dis[N],father[N];
void isit(int n)
{
    for(int i = 0;i<=n;i++)
    {
        v[i].clear();
        query[i].clear();
        num[i].clear();
        vis[i] = false;
        dis[i] = 0;
        father[i] = i;
    }
}
int find(int x)
{
    if(x==father[x])
        return x;
    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 o,int val)
{
    vis[o] = true;
    dis[o] = val;
    for(int i = 0; i < v[o].size();i++)
    {
        Node tmp = v[o][i];
        if(vis[tmp.to])
            continue;
        Tarjan(tmp.to,val+tmp.val);
        Union(o,tmp.to);
    }
    for(int i=0;i<query[o].size();i++)
    {
        int tmp = query[o][i];
        if(vis[tmp])
            ans[num[o][i]] = dis[o] + dis[tmp] - 2*dis[find(tmp)];
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        isit(n);
        for(int i = 1;i < n; i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            v[x].push_back((Node){y,z});
            v[y].push_back((Node){x,z});
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            query[x].push_back(y);
            num[x].push_back(i);
            query[y].push_back(x);
            num[y].push_back(i);
        }
        Tarjan(1,0);
        for(int i = 0;i<m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}
时间: 2024-07-31 15:42:00

hdoj 2586 How far away ? 【Tarjan离线LCA】的相关文章

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

SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight. We will ask you to perform the following operation: u v k : ask for the kth minimum weight on the path from node u 

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 ?(Tarjan离线LCA)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:对于一个有 n 个节点的图,有 n - 1 条无向边,权值给出.有 m 个查询, 每个查询 a b 表示询问 a b 两节点间的距离. 思路: 把这个联通图以树的形式表现出来,取任意两点,假设其最近公共祖先(Least Common Ancestors)为 lca,则两点间的距离等于: dis(a, b) = dis(a, root) + dis(b, root) - 2 * dis(r

hdoj 2874 Connections between cities 【Tarjan离线LCA】

题目:hdoj 2874 Connections between cities 题意:战争过后,一些城市毁坏了.意思图不连通,让你求任意两点的距离. 分析:很明显求LCA 但是图不连通,所以我们Tarjan的时候要对每个点进行.然后标记即可. 另外,这个题目卡vector,看来以后要学着用数组模拟邻接表了. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc

poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】

题目:poj 1470 Closest Common Ancestors 题意:给出一个树,一些询问.求LCA的个数. 分析:很简单的模板题目,但是模板不够优秀,一直wa...RE,各种错误一下午,终于发现自己模板的漏洞了. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 1010 #

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

POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)

Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of