zoukankan      html  css  js  c++  java
  • hdu5266 pog loves szh III 【LCA】【倍增】

    Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree.Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor (LCA) of these node.The question is too difficult for Pog.So he decided to simplify the problems.The nodes picked are consecutive numbers from lili to riri ([li,ri])([li,ri])

    Hint : You should be careful about stack overflow !
    InputSeveral groups of data (no more than 3 groups,n10000n≥10000 or Q10000Q≥10000). 

    The following line contains ans integers,n(2n300000)n(2≤n≤300000)

    AT The following n1n−1 line, two integers are bibi and cici at every line, it shows an edge connecting bibi and cici

    The following line contains ans integers,Q(Q300000)Q(Q≤300000)

    AT The following QQ line contains two integers li and ri(1lirin1≤li≤ri≤n).OutputFor each case,output QQ integers means the LCA of [li,ri][li,ri].Sample Input
    5
    1 2
    1 3
    3 4
    4 5
    5
    1 2
    2 3
    3 4
    3 5
    1 5
    Sample Output
    1
    1
    3
    3
    1
    
    
    
            
      
    Hint
    Be careful about stack overflow.
            

    用BFS 预处理不会爆栈  注意看n的范围 看到前面的1e5就以为是1e5了

    f[i][j]表示节点i的第2^j个祖先 

    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <set>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int n, q, ecnt;
    const int maxn = 300005;
    struct edge{
        int v, next;
    }e[maxn << 1];
    int dep[maxn], f[20][maxn], head[maxn];
    
    
    void bfs(int rt)
    {
        queue<int> q;
        q.push(rt);
        f[0][rt] = rt;
        dep[rt] = 0;
        while(!q.empty()){
            int tmp = q.front();
            q.pop();
            for(int i = 1; i < 20; i++){
                f[i][tmp] = f[i - 1][f[i - 1][tmp]];
            }
            for(int i = head[tmp]; i != -1; i = e[i].next){
                int v = e[i].v;
                if(v == f[0][tmp]) continue;
                dep[v] = dep[tmp] + 1;
                f[0][v] = tmp;
                q.push(v);
            }
        }
    }
    
    int LCA(int u, int v)
    {
        if(dep[u] > dep[v]) swap(u, v);
        int hu = dep[u], hv = dep[v];
        int tu = u, tv = v;
        for(int det = hv - hu, i = 0; det; det >>= 1, i++){
            if(det & 1){
                tv = f[i][tv];
            }
        }
        if(tu == tv){
            return tu;
        }
        for(int i = 19; i >= 0; i--){
            if(f[i][tu] == f[i][tv]){
                continue;
            }
            tu = f[i][tu];
            tv = f[i][tv];
        }
        return f[0][tu];
    
    }
    
    void init()
    {
        memset(head, -1, sizeof(head));
        ecnt = 0;
    }
    
    void adde(int u, int v)
    {
        e[ecnt].v = v;
        e[ecnt].next = head[u];
        head[u] = ecnt++;
    }
    
    int dp[maxn][20];
    int main()
    {
        while(scanf("%d", &n) != EOF){
            init();
            for(int i = 1; i < n; i++){
                int x, y;
                scanf("%d%d", &x, &y);
                adde(x, y);
                adde(y, x);
            }
            bfs(1);
            for(int i = 1; i <= n; i++){
                dp[i][0] = i;
            }
            for(int j = 1; j < 20; j++){
                for(int i = 1; i + (1 << j) - 1 <= n; i++){
                    dp[i][j] = LCA(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
                }
            }
    
            scanf("%d", &q);
            while(q--){
                int l, r;
                scanf("%d%d", &l, &r);
                int k = (int)log2(r - l + 1);
                cout<<LCA(dp[l][k], dp[r - (1 << k) + 1][k])<<endl;
            }
        }
        return 0;
    }
    




  • 相关阅读:
    [Javascript] twitterbootstrap: 解决2.1版本中modal加载远程内容时,只加载一次的问题
    向大型网站学习SEO优化之道
    [Yii Framework] 使用Yii Framework开发微信公众平台
    [jQuery] ajax跨域处理方式
    [jQuery] form提交到iframe之后,获取iframe里面内容
    [Ubuntu] fatal: recursion detected in die handler
    [Yii Framework] Yii如何实现前后台的session分离
    [ubuntu] ubuntu13.04 64bit,安装FastDFS4.06过程遇到的问题和解决方案
    [转] 怎样在Ubuntu上安装Git服务器
    [ubuntu] ubuntu13.04切换桌面/工作区的方法
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643403.html
Copyright © 2011-2022 走看看