LCA模板整理

HDU2586 纯LCA模板

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define lowbit(x) x&(-x)
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define ls o<<1
#define rs o<<1|1
#define lson L,mid,ls
#define rson mid+1,R,rs
#define fi first
#define se second
using namespace std;
inline char nc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _read() {
    char ch = nc(); int sum = 0;
    while (!(ch >= ‘0‘&&ch <= ‘9‘))ch = nc();
    while (ch >= ‘0‘&&ch <= ‘9‘)sum = sum * 10 + ch - 48, ch = nc();
    return sum;
}

typedef long long ll;
const int maxn = 4e4+10;
const double eps = 1e-8;
const int mod = 998244353;
const double pi = acos(-1.0);

vector<pair<int,int>> G[maxn];
int dep[maxn],dis[maxn],par[maxn][20];

void dfs(int u,int fa,int dp,int ds)
{
    dep[u]=dp;
    dis[u]=ds;
    if(u==1)
    {
        for(int i=0;i<20;i++)par[u][i]=1;
    }
    else
    {
        par[u][0]=fa;
        for(int i=1;i<20;i++)
        {
            par[u][i]=par[par[u][i-1]][i-1];
        }
    }
    for(auto e:G[u])
    {
        int v=e.fi,c=e.se;
        if(v==fa) continue;
        dfs(v,u,dp+1,ds+c);
    }
}

int Jump(int u,int ds)
{
    for(int j=19;j>=0;j--)
    {
        if((1<<j)&ds)
        {
            u=par[u][j];
        }
    }
    return u;
}

int lca(int u,int v)
{
    if(dep[u]<dep[v]) swap(u,v);
    //u 深度较大
    u=Jump(u,dep[u]-dep[v]);
    if(u==v) return u;
    for(int i=19;i>=0;i--)
    {
        if(par[u][i]!=par[v][i])
        {
            u=par[u][i];
            v=par[v][i];
        }
    }
    return par[u][0];
}

int main()
{
    int T;
    T=_read();
    while(T--)
    {
        int n,m;
        n=_read(),m=_read();
        for(int i=1;i<n;i++)
        {
            int u,v,c;
            u=_read(),v=_read(),c=_read();
            G[u].push_back(make_pair(v,c));
            G[v].push_back(make_pair(u,c));
        }
        dfs(1,1,0,0);
        for(int i=1;i<=m;i++)
        {
            int u,v;
            u=_read(),v=_read();
            int ca=lca(u,v);
            printf("%d\n",dis[u]+dis[v]-2*dis[ca]);
        }
    }
}

原文地址:https://www.cnblogs.com/WWkkk/p/9504302.html

时间: 2024-10-27 11:59:26

LCA模板整理的相关文章

HDU 5296 Annoying problem(LCA模板+树的dfs序心得)

Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,-,n, which has n-1 edge,each edge has a weight. An existing set S is initially empty. Now there are two kinds of operation: 1 x: If the node x is not in the set S, add n

算法复习——LCA模板(POJ1330)

题目: 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 ancesto

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

LCA模板(数剖实现)

题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n),每次查询为logn,因此总复杂度为:O(2*n+m*logn). 代码: #include<cstdio> #include<cstring> using namespace std; const int maxn=500005; struct node{ int v,next; }

POJ 1330(LCA模板)

链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<string> 7 #incl

手机客户端接口开发文档模板整理

这是个人整理的手机开发文档模板,方便自己以后编写文档. 大体内容如下,详细在个人csdn中下载: 移动端转发短信上传温湿度信息 移动端负责后台接收温湿度传感器通过短信发来的温湿度和经纬度信息,移动端后台接收后上传web服务器,当然传感器也可通过wifi直接上传web服务器.返回小写true或者false. 请求URL: http://192.168.1.101:8080/RFID/addTransTemperature.action?phoneNumber=123&temperature=12.

hdu 2586 How far away? (LCA模板)

题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhead[maxm]. 输出用%I64d,不要用%lld. C++ RE后 尝试用 G++交. 代码: struct node{ int to,w,next,lca; }; int const maxn = 40005; int const maxm = 205; int fa[maxn]; int he

hihoCoder_#1067_最近公共祖先&#183;二(LCA模板)

#1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣--或者说粗糙的手段山寨出了一个神奇的网站,这个网站可以计算出某两个人的所有共同祖先中辈分最低的一个是谁.远在美国的他们利用了一些奇妙的技术获得了国内许多人的相关信息,并且搭建了一个小小的网站来应付来自四面八方的请求. 但正如我们所能想象到的--这样一个简单的算法并不能支撑住非常大的访问量,所以摆在小Hi和小Ho面前的无非两种选择: 其一是购买更为昂

HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house