HDU2586 How far away ?(LCA模板题)

题目链接:传送门

题意:

给定一棵树,求两个点之间的距离。

分析:

LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)];

在线算法:具体讲解 传送门

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 40010;

struct nod{
    int to,next,w;
}edge[maxn*2];

int head[maxn],ip,tot;
bool vis[maxn];
int R[maxn*2],ver[maxn*2];
int dp[maxn*2][25];
int first[maxn];
int dis[maxn];

void init(){
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    dis[1]=0,ip=0,tot=0;
}

void add(int u,int v,int w){
    edge[ip].to=v;
    edge[ip].w=w;
    edge[ip].next=head[u];
    head[u]=ip++;
}
/***
ver[i]=x:第i个点是x.
first[i]=x: 点i第一次出现的位置是x
R[i]=x:第i个点的深度为x;
dis[i]=x;点i到根节点的距离为x.
***/
void dfs(int u,int dept){
    vis[u]=true,ver[++tot]=u,first[u]=tot,R[tot]=dept;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            dis[v]=dis[u]+edge[i].w;
            dfs(v,dept+1);
            ver[++tot]=u,R[tot]=dept;
        }
    }
}

void ST(int n){
    for(int i=1;i<=n;i++) dp[i][0]=i;
    for(int i=1;(1<<i)<=n;i++){
        for(int j=1;j+(1<<i)<=n;j++){
            int a = dp[j][i-1],b=dp[j+(1<<(i-1))][i-1];
            if(R[a]<R[b]) dp[j][i]=a;
            else dp[j][i]=b;
        }
    }
}

int RMQ(int l,int r){
    int k=0;
    while(1<<(k+1)<=r-l+1)
        k++;
    int x = dp[l][k], y=dp[r-(1<<k)+1][k];
    if(R[x]<R[y]) return x;
    else return y;
}

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

int main(){
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<n-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        dfs(1,1);
        ST(2*n-1);
        for(int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d\n",dis[u]+dis[v]-2*dis[LCA(u,v)]);
        }
    }
    return 0;
}

离线算法: 具体讲解 传送门

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 40010;

struct nod{
    int u,v,next,w,lca;
}edge[maxn*2],edge1[maxn];

int par[maxn],ancestors[maxn];
int head[maxn],head1[maxn];
int dis[maxn],ip,ip1;
bool vis[maxn];

void init(){
    memset(head1,-1,sizeof(head1));
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    for(int i=1;i<maxn;i++) par[i]=i;
    dis[1]=0,ip1=0,ip=0;
}

int find_par(int x){
    if(x!=par[x]) return par[x]=find_par(par[x]);
    return par[x];
}

void Union(int u,int v){
    u=find_par(u);
    v=find_par(v);
    if(u!=v) par[v]=u;
}

void add(int u,int v,int w){
    edge[ip].v=v;
    edge[ip].w=w;
    edge[ip].next=head[u];
    head[u]=ip++;
}

void add1(int u,int v){
    edge1[ip1].u=u;
    edge1[ip1].v=v;
    edge1[ip1].lca=-1;
    edge1[ip1].next=head1[u];
    head1[u]=ip1++;
}

void tarjan(int u){
    vis[u]=1;
    ancestors[u]=par[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;
            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=ancestors[find_par(v)];
        }
    }
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<n-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        for(int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add1(u,v);
            add1(v,u);
        }
        tarjan(1);
        for(int i=0;i<m;i++){
            printf("%d\n",dis[edge1[i*2].u]+dis[edge1[i*2].v]-2*dis[edge1[i*2].lca]);
        }
    }
    return 0;
}
时间: 2024-10-11 11:31:17

HDU2586 How far away ?(LCA模板题)的相关文章

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

poj1470(LCA模板题)

题目连接:http://poj.org/problem?id=1470 被输入卡到死...... 开始也看到输入格式的问题了,但是我以为查询的两个点的格式就是(u v)这样,其它地方可以无限空格... 为这一直WA啊,以为是算法哪块写挂了,一直找..... 其实哪都可能有一堆空格(其实题目说的也很清楚了-_-||).... 教训.. 还有一点,刚开始以为给出的第一个节点就是祖先,其实题目并没有这么说,看着图理解就误以为了那样了... 1 #include<cstdio> 2 #include&

HDU 2586 LCA模板题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 设dist[i]为i到根0的链和,求法(Dfs过程中dist[v]=dist[u]+e[i].w) 对于树中任意两点形成的链,可以通过LCA最近公共祖先剖分. 比如2->3,就可以经过LCA点1:  2->1->3 链和=dist[u]+dist[v]-2*dist[LCA[u,v]] (

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

[jzyzoj2021]lca模板题

查找最近公共祖先...我也不知道这东西有什么用,在线写法,非常之慢.... 存代码 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 using namespace std; 7 int n,m; 8 struct nod{ 9 int y; 10 int next; 11 }e[200020]

[hdu2586]How far away?(LCA)

题意:问树上两点之间的最短距离 解题关键:LCA模板题,不知道为什么在hdu g++超时,c++70ms就过了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cmath> 6 #include<iostream> 7 typedef long long ll; 8 using namespace

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

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