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,n≥10000n≥10000 or Q≥10000Q≥10000). 

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

AT The following n−1n−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(Q≤300000)Q(Q≤300000). 

AT The following QQ line contains two integers li and ri(1≤li≤ri≤n1≤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;
}

hdu5266 pog loves szh III 【LCA】【倍增】的更多相关文章

  1. HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )

    pog loves szh III Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  2. HDU 5266 pog loves szh III (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...

  3. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  4. HDU 5266 pog loves szh III(区间LCA)

    题目链接 pog loves szh III 题意就是  求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...

  5. HDU 5266 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 ...

  6. HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)

    题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...

  7. HDU 5266 pog loves szh III

    题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...

  8. hdu 5265 pog loves szh II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...

  9. hdu 5264 pog loves szh I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...

随机推荐

  1. Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等

    下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...

  2. 恶劣条件下的apache配置(Linux)

    (本文出自yangjj ^_^)   前提:1.没联网,yum挂.2.至少要有GCC,要不玩个屁. 3.你有充足的咖啡并且有几个小时时间不想打dota. 4.你要做集群. 以上条件不满足其一,看到这里 ...

  3. Linux+Redis实战教程_day01_Linux介绍与安装

    1.Linux介绍(了解) 1.1.Linux和Windows的区别 Linux是一款操作系统.正规开发 服务器项目部署都是放在Linux操作系统上. Windows一款操作系统,民用操作系统.娱乐. ...

  4. Python对象(上)

    Python是一门面向对象的语言,在Python中一切都是对象,函数是对象,类型也是对象. 下面就看看Python中对象相关的内容. Python对象基本概念 Python对象有三个基本的要素: 身份 ...

  5. hydra 及相关示例

    http://www.cnblogs.com/mchina/archive/2013/01/01/2840815.html https://www.thc.org/thc-hydra/ 语法 # hy ...

  6. Win10 我的电脑 -- 右键点击管理打不开

    右键点击我的电脑 -- 管理,出现如下错误,这是删除快捷方式小箭头导致的 解决方法: win+R 输入 regedit,分别在 HKEY_CLASSES_ROOT\piffile HKEY_CLASS ...

  7. [Linux] 修改用户名密码

    1. 普通用户或root用户修改自身登录密码:在终端使用passwd命令. linaro@linaro-ubuntu-desktop:~$ passwd Changing password for l ...

  8. Kafka manager安装 (支持0.10以后版本consumer)

    下载地址:  https://pan.baidu.com/s/1jIE3YL4 步骤: 1. 解压kafka-manager-1.3.2.1.zip 2. cd kafka-manager-1.3.2 ...

  9. Python中定义函数时参数有默认值的小陷阱

    在定义函数的时候,如果函数的参数有默认值,有两种类型的参数,一种是整数,字符串这种不可变类型,另一种是列表这种可变类型,对于第一种情况没有什么特殊的地方,但是对于可变类型,有一个微妙的小陷阱. 可变类 ...

  10. hadoop参数传递

    传参关键代码: //从配置文件获取参数,必须在作业创建的前面 conf.addResource("hadoop-bigdata.xml"); keepUrl=conf.get(&q ...