PJOI PKU Campus 2011 B:A Problem about Tree LCA 求随意点x为根的y的父节点
题目链接:点击打开链接
题意:给定n个点 m个询问
以下n-1行给定一棵树
m个询问 x y
问把树转成以x为根 y的父节点是谁
第一种情况lca==y那就是x的第 dep[x] - dep[y] -1 父亲,依次向上爬山坡,利用倍增的二进制加速。
另外一种就是Father[y];
#include"cstdio"
#include"iostream"
#include"queue"
#include"algorithm"
#include"set"
#include"queue"
#include"cmath"
#include"string.h"
#include"vector"
using namespace std;
#define N 10050
#define e 2.718281828459
struct Edge{
int from, to, dis, nex;
}edge[5*N];
int head[N],edgenum,dis[N],fa[N][20],dep[N];
void add(int u,int v,int w){
Edge E={u,v,w,head[u]};
edge[edgenum] = E;
head[u]=edgenum++;
}
void bfs(int root){
queue<int> q;
fa[root][0]=root;dep[root]=0;dis[root]=0;
q.push(root);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u]; ~i;i=edge[i].nex){
int v=edge[i].to;if(v==fa[u][0])continue;
dep[v]=dep[u]+1;dis[v]=dis[u]+edge[i].dis;fa[v][0]=u;
q.push(v);
}
}
}
int Lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=0;i<20;i++)if((dep[x]-dep[y])&(1<<i))x=fa[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
void init(){memset(head, -1, sizeof head); edgenum = 0;}
int n, m;
int main(){
int T, i, j, x, y;scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init();
for(i=1;i<n;i++){scanf("%d %d",&x,&y);add(x,y,1);add(y,x,1);}
bfs(1);
while(m--){
scanf("%d %d",&x,&y);
int lca = Lca(x,y);
if(lca==y){
int D = dep[x] - dep[y]-1;
while(D){
int z = (int)(log10(1.0*D)/log10(2.0));
x = fa[x][z];
D-=1<<z;
}
printf("%d\n",x);
}
else printf("%d\n",fa[y][0]);
}
}
return 0;
}
PJOI PKU Campus 2011 B:A Problem about Tree LCA 求随意点x为根的y的父节点的更多相关文章
- PKU Campus 2015
PKU Campus 2015 B 注意到竖着落下不改变列模 4 的结果.问题转化为:模 4 系下,给序列,可选长度为 4 子区间,区间加一,能否让所有元素相等. C.Rabbit's Festiva ...
- HYNB Round 15: PKU Campus 2019
HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...
- PKU campus 2018 A Wife——差分约束?/dp
题目:http://poj.openjudge.cn/campus2018/A 有正规的差分约束做法,用到矩阵转置等等. 但也有简单(?)的dp做法. 有一个结论(?):一定要么在一天一点也不选,要么 ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Problem F: Exponentiation大数求幂
DescriptionProblems involving the computation of exact values of very large magnitude and precision ...
- You can Solve a Geometry Problem too(线段求交)
http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000 ...
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- HDU 5296 Annoying problem dfs序 lca set
Annoying problem Problem Description Coco has a tree, whose nodes are conveniently labeled by 1,2,…, ...
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
随机推荐
- Spring boot 文件路径读取异常
在开发代码中,有一段需要获取resources目录下的一个配置文件(这里写作test.xml). 这段代码在ide中没有任何问题,但是一打成jar包发布到线上,这段代码就会报找不到对应文件的错误. 按 ...
- tushare 安装
1.pip install lxml 2.pip install pandas 3.pip install requests 4.pip install lxml 5.pip install tush ...
- 关系和非关系型数据库区别(以及oracle和mysql的区别)
一.关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库. 关系模型是在1970年由IBM的研究员E.F.Codd博士首先提出的,在之后的几十年中,关系模型的概念得到了充分的发展并逐 ...
- windows下tortoiseGit安装和使用
一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 图太多就不继 ...
- C#实现HTML转图片(网页快照)
有时候我们需要将网页转成图片,那么可以使用WebBrowser来生成网页快照,废话不多说,代码如下 1.网页快照帮助类(如果是BS或控制台需要引用System.Windows.Forms类库): pu ...
- 跳转的两种实现方法setInterval和setTimeout
setInterval方法: <html> <head> <meta http-equiv="Content-Type" content=" ...
- 【互动问答分享】第18期决胜云计算大数据时代Spark亚太研究院公益大讲堂
Q1:Master和Driver的是同一个东西吗? 两者不是同一个东西,在Standalone模式下Master是用于集群资源管理和调度的,而Driver适用于指挥Worker上的Executor通过 ...
- python+selenium 组织用例方式 总结
1.unittest.main() 将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行他们.执行 ...
- django CXRF介绍
CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...
- linux中Firefox浏览器 手动安装 flash
打开firefox浏览器,当你打开有关音频或者视频的网站时候,会提示你安装 flash,可是,官网提示,需要手动安装. 1.先从提示的官网上下载好文件 “install_flash_player_11 ...