LCA板子+应用例题

来个板子

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 5e5 + 7;
const int maxm = 5e4 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
int head[maxn],tot;
int n,m,s,f[maxn][30],lg[maxn],h[maxn];
struct edge{
    int to,next;
}e[maxn<<1];
void add(int u,int v){
    e[++tot].to=v;
    e[tot].next=head[u];
    head[u]=tot;
}
void dfs(int x,int fa){
    h[x]=h[fa]+1;//子节点深度比父节点大一
    f[x][0]=fa;//初始化当前节点父亲为自己
    for(int i=1;(1<<i)<=h[x];i++)f[x][i]=f[f[x][i-1]][i-1];//从下往上倍减找父节点
    for(int i=head[x];i;i=e[i].next){                      //意思是f的2^i祖先等于f的2^(i-1)祖先的2^(i-1)祖先
        if(e[i].to!=fa)dfs(e[i].to,x);//找到叶子节点前一直递归
    }
}
int LCA(int x,int y){
    if(h[x]<h[y])swap(x,y);//假设x是最深的节点
    while(h[x]>h[y])x=f[x][lg[h[x]-h[y]]-1];//让两个节点一样深
    if(x==y)return x;
    for(int i=lg[h[x]]-1;i>=0;i--){
        if(f[x][i]!=f[y][i]){//如果不一样那么肯定没有到达lca ,因为两个节点的lca 向上的节点就是一样的了
            x=f[x][i],y=f[y][i];
        }
    }
    return f[x][0];
}
int main(){
    scanf("%d %d %d",&n,&m,&s);
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(s,0);
    for(int i=1;i<=n;i++)lg[i]=lg[i-1]+(1<<lg[i-1]==i);//预处理(不太懂)
    while(m--){
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",LCA(a,b));
    }
    return 0;
}

昨天做了一个LCA的题,感觉挺不错的,记录一下。
Bond
题目大意
给出N座城市,M条双向道路,以及每一条道路上所需要的花费。
给出Q次询问,每次询问两座城市st以及en,我们需要求出所有从st到en的最短路径上的最大边长。
题解:LCA+MST(最小瓶颈路)
AC代码

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<double,int> P;
const int maxn = 5e4 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
struct node1{//链式前向星存图
    int u,v,f;
    bool operator <(const node1 &a)const{
        return a.f<f;
    }
}qu[maxn*2];
struct node2{//最小生成树节点,邻接表存图
    int x,y;//x表示下一个节点,y表示该节点到x这条边的权值
    node2(int x=0,int y=0):x(x),y(y){};
};
vector<node2>a[maxn];//邻接表存图
int n,m,pre[maxn],fa[maxn],rk[maxn],cost[maxn];//pre[x]表示x的父亲(并查集),fa[x]表示x的父亲(MST),rk[x]表示节点x的深度,cost[x]表示MST上边的权值
int anc[maxn][30],maxcost[maxn][30];
int find(int x){//找到父亲
    while(x!=pre[x])x=pre[x];
    return x;
}
void dfs(int u,int f,int depth){//DFS预处理MST
    rk[u]=depth;//记录当前节点的深度
    fa[u]=u;//将当前节点父亲初始化为自己
    for(int i=0;i<a[u].size();i++){
        int v=a[u][i].x;
        int w=a[u][i].y;
        if(v!=f){//未遍历到父节点,继续DFS
            dfs(v,u,depth+1);
            fa[v]=u;//回溯更新已遍历到的节点的父亲
            cost[v]=w;//更新根节点到V的距离
        }
    }
}
void lca(){
    for(int i=1;i<=n;i++){//anc[i][j]表示i节点的第2^j级祖先
        anc[i][0]=fa[i];//将父节点初始化
        maxcost[i][0]=cost[i];//权值初始化
        for(int j=1;(1<<j)<=n;j++){//还没有父亲
            anc[i][j]=-1;
        }
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i<=n;i++){
            if(anc[i][j-1]!=-1){
                anc[i][j]=anc[anc[i][j-1]][j-1];//倍增找父亲
                maxcost[i][j]=max(maxcost[i][j-1],maxcost[anc[i][j-1]][j-1]);//更新i的第2^j级祖先的最大权值
            }
        }
    }
}
int query(int x,int y){
    if(rk[x]<rk[y])swap(x,y);//默认x深度更深
    int k,ans=-inf;
    for(k=1;(1<<(k+1)<=rk[x]);k++);//找到级数
    for(int i=k;i>=0;i--){//从x的深度找到y的深度
        if(rk[x]-(1<<i)>=rk[y]){
            ans=max(ans,maxcost[x][i]);
            x=anc[x][i];
        }
    }
    if(x==y)return ans;//如果y就是x的祖先,那么直接返回答案
    for(int i=k;i>=0;i--){//如果y不是x的祖先
        if(anc[x][i]!=-1&&anc[x][i]!=anc[y][i]){//x和y一起向上找祖先并更新答案
            ans=max(ans,maxcost[x][i]);
            x=anc[x][i];
            ans=max(ans,maxcost[y][i]);
            y=anc[y][i];
        }
    }
    ans=max(ans,max(cost[x],cost[y]));//答案取最大值
    return ans;
}
int main(){
    int flag=1;
    while(scanf("%d%d",&n,&m)!=EOF){
        if(flag==0)printf("\n");
        flag=0;
        for(int i=0;i<m;i++){//边
            scanf("%d%d%d",&qu[i].u,&qu[i].v,&qu[i].f);
        }
        sort(qu,qu+m);//排序
        for(int i=0;i<=n;i++){
            a[i].clear();
            pre[i]=i;
        }
        for(int i=0;i<m;i++){//建图
            int x=find(qu[i].u);
            int y=find(qu[i].v);
            if(x!=y){
                pre[x]=y;
                a[x].push_back(node2(y,qu[i].f));
                a[y].push_back(node2(x,qu[i].f));
            }
        }
        dfs(1,-1,0);//LCA预处理
        lca();//LCA
        int q;
        scanf("%d",&q);
        while(q--){
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%d\n",query(x,y));
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LCA+路径压缩的方式可以用于求解树上的桥,具体实现步骤如下: 1. 对于树上每个节点,记录其在树中的深度(或者高度)以及其父亲节点。 2. 对于每个节点,记录其在树上的最小深度(或最小高度)以及其所在子树中深度最小的节点。 3. 对于每条边(u, v),设u的深度小于v的深度(或者高度),则如果v的子树中没有深度小于u的节点,则(u, v)是桥。 具体的实现过程如下: 首先,我们需要对树进行预处理,求出每个节点的深度以及其父亲节点。可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来实现。在这里我们使用DFS来实现: ```c++ vector<int> adj[MAX_N]; // 树的邻接表 int n; // 树的节点数 int dep[MAX_N], fa[MAX_N]; // dep[i]表示节点i的深度,fa[i]表示节点i的父亲节点 void dfs(int u, int f, int d) { dep[u] = d; fa[u] = f; for (int v : adj[u]) { if (v != f) { dfs(v, u, d + 1); } } } ``` 接下来,我们需要计算每个节点所在子树中深度最小的节点。我们可以使用LCA(最近公共祖先)的方法来实现。具体来说,我们可以使用倍增算法来预处理出每个节点的2^k级祖先,并且在查询LCA时使用路径压缩的方式优化时间复杂度。这里我们不展开讲解LCA和倍增算法的细节,如果你对此感兴趣,可以参考其他资料进行学习。 ```c++ const int MAX_LOG_N = 20; // log2(n)的上取整 int anc[MAX_N][MAX_LOG_N]; // anc[i][j]表示节点i的2^j级祖先 int mn[MAX_N]; // mn[i]表示节点i所在子树中深度最小的节点 void precompute() { // 预处理anc数组 for (int j = 1; j < MAX_LOG_N; j++) { for (int i = 1; i <= n; i++) { if (anc[i][j - 1] != -1) { anc[i][j] = anc[anc[i][j - 1]][j - 1]; } } } // 计算mn数组 for (int i = 1; i <= n; i++) { mn[i] = i; for (int j = 0; (1 << j) <= dep[i]; j++) { if ((dep[i] & (1 << j)) != 0) { mn[i] = min(mn[i], mn[anc[i][j]]); i = anc[i][j]; } } } } ``` 最后,我们可以使用LCA+路径压缩的方式来判断每条边是否为桥。具体来说,对于每条边(u, v),我们需要判断v的子树中是否存在深度小于u的节点。如果存在,则(u, v)不是桥,否则(u, v)是桥。 ```c++ bool is_bridge(int u, int v) { if (dep[u] > dep[v]) swap(u, v); if (mn[v] != u) return true; // 子树中存在深度小于u的节点 return false; // 子树中不存在深度小于u的节点 } ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值