zoukankan      html  css  js  c++  java
  • 树上倍增求LCA(洛谷P3379)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5e5+10;
    void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
    }
    void o(int x) {
    if (x < 0){
    putchar('-');
    x = -x;
    }
    if (x > 9)o(x / 10);
    putchar(x % 10 + '0');
    }
    int read(){
    int res=0;
    int y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){res=(res<<1)+(res<<3)+c-'0';c=getchar();}
    return res*y;
    }
    int n,m,s,t;
    int a,b;//qr
    vector<int>g[maxn];
    void add(int fr,int to){
    g[fr].push_back(to);
    g[to].push_back(fr);
    }
    int f[maxn][30];
    int depth[maxn];
    void bfs(){
    queue<int>q;
    q.push(s);
    depth[s]=1;
    f[s][0]=s;
    while (!q.empty()){
    int u=q.front();q.pop();
    for(int i=0;i<g[u].size();i++){
    int v=g[u][i];
    if(depth[v])continue;
    depth[v]=depth[u]+1;
    f[v][0]=u;
    for(int i=1;i<=t;i++){
    f[v][i]=f[f[v][i-1]][i-1];
    }
    q.push(v);
    }
    }
    }
    int lca(int c,int d){
    if(depth[c]>depth[d])swap(c,d);// depth d >= c
    for(int i=t;i>=0;i--){
    if(depth[f[d][i]]>=depth[c])d=f[d][i];
    }
    if(c==d)return c;
    for(int i=t;i>=0;i--){
    if(f[c][i]!=f[d][i]){
    c=f[c][i];d=f[d][i];
    }
    }
    return f[c][0];
    }
    signed main(){
    in(n);in(m);in(s);
    t=log2(n);
    for(int i=1;i<=n-1;i++)add(read(),read());
    bfs();
    while(m--){
    in(a);in(b);
    o(lca(a,b));putchar(' ');
    }
    return 0;
    }
  • 相关阅读:
    HBase 安装设置
    Python中通过函数对象创建全局变量
    Scala 中 构造函数,重载函数的执行顺序
    Hive 安装配置
    976. 三角形的最大周长
    922. 按奇偶排序数组 II
    350. 两个数组的交集 II
    349. 两个数组的交集
    242. 有效的字母异位词
    925. 长按键入
  • 原文地址:https://www.cnblogs.com/yesuweiYYYY/p/13809428.html
Copyright © 2011-2022 走看看