zoukankan      html  css  js  c++  java
  • LCA 树链剖分

    //LCA
    //树链剖分 在线 
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int n,m,root,cnt,head[500001];
    int hvyson[500001],fa[500001],siz[500001],dep[500001],top[500001];
    struct uio{
        int to,next;
    }edge[1000001];
    void add(int x,int y)
    {
        edge[++cnt].next=head[x];
        edge[cnt].to=y;
        head[x]=cnt;
    }
    void dfs1(int x,int f,int depth)
    {
        dep[x]=depth;
        fa[x]=f;
        siz[x]=1;
        int maxson=-1;
        for(int i=head[x];i;i=edge[i].next)
        {
            int y=edge[i].to;
            if(y==f)
                continue;
            dfs1(y,x,depth+1);
            siz[x]+=siz[y];
            if(siz[y]>maxson)
            {
                hvyson[x]=y;
                maxson=siz[y];
            }
        }
    }
    void dfs2(int x,int topf)
    {
        top[x]=topf;
        if(!hvyson[x])
            return;
        dfs2(hvyson[x],topf);
        for(int i=head[x];i;i=edge[i].next)
        {
            int y=edge[i].to;
            if(y==fa[x]||y==hvyson[x])
                continue;
            dfs2(y,y);
        }
    }
    int lca(int x,int y)
    {
        while(top[x]!=top[y])
        {
            if(dep[top[x]]<dep[top[y]])
                swap(x,y);
            x=fa[top[x]];
        }
        return dep[x]<dep[y]? x : y;
    }
    void do_something()
    {
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d
    ",lca(u,v));
        }
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&root);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs1(root,0,1);
        dfs2(root,root);
        do_something();
         return 0;
    }
  • 相关阅读:
    macOS 常用配置
    maven 常用配置
    log4j(1.x)最全配置!!!
    Python2 中 input() 和 raw_input() 的区别
    用 Maven 构建 Java-Scala 混合项目
    trim() 的秘密
    【目录】一起来学 Kafka 吧
    Spring in Action.4th
    Hibernate快速入门
    html2canvas 跨域图片无法正常加载问题解决办法
  • 原文地址:https://www.cnblogs.com/water-radish/p/9280519.html
Copyright © 2011-2022 走看看