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& ...
随机推荐
- android studio 设置
1.设置启动不打开最近项目 2.设置字体 3.安装逍游模拟器,并与android studio 进行链接 adb connect 127.0.0.1:21503 4.添加第三方包 文件jar.Modu ...
- windows下tortoiseGit安装和使用
一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 图太多就不继 ...
- hdu5739
以前从来没写过求点双连通分量,现在写一下…… 这题还用到了一个叫做block forest data structure,第一次见过…… ——对于每一个点双联通分量S, 新建一个节点s, 向S中每个节 ...
- radio和label关联问题,点击label改变颜色
$(function () { $("#fangan :radio[name='price']").bind('click', function (event) { //$(thi ...
- [jquery] ajax parsererror
http://stackoverflow.com/questions/5061310/jquery-returning-parsererror-for-ajax-request 方法一: 直接去掉 d ...
- POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...
- HDU 5695 Gym Class && 百度之星 初赛 1006
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5695 本文链接:http://www.cnblogs.com/Ash-ly/p/5515234.htm ...
- HDOJ 2582 f(n)
Discription This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+… ...
- 【博弈论】bzoj1115 [POI2009]石子游戏Kam
差分后与阶梯博弈很类似. #include<cstdio> using namespace std; int n,T,a[1001],ans; int main() { scanf(&qu ...
- 【最大流】【Dinic】bzoj1711 [Usaco2007 Open]Dingin吃饭
把牛拆点,互相连1的边. 把牛的食物向牛连边,把牛向牛的饮料连边. 把源点向牛的食物连边,把牛的饮料向汇点连边. 要把牛放在中间,否则会造成一头牛吃了自己的食物后又去喝别的牛的饮料的情况. #incl ...