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的父节点

时间: 2024-10-10 09:09:04

PJOI PKU Campus 2011 B:A Problem about Tree LCA 求任意点x为根的y的父节点的相关文章

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" #

HYNB Round 15: PKU Campus 2019

HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归约到两行上,再从左到右依次考虑即可(考虑每条分界线被经过多少次) 比赛时被贪心治了... G. Go and Oreo 题意 把n*n个格子黑白染色后,数奥利奥 做法 按格考虑,简易版轮廓线DP 赛中一直在按行考虑,然后就不会了 HYNB Round 15: PKU Campus 2019 原文地址

Problem Binary Tree Preorder Traversal

Problem  Description: Given a binary tree, return the preorder traversal of its nodes' values.  Solution: 1 public List<Integer> preorderTraversal(TreeNode root) { 2 if (root == null) return new LinkedList<Integer>(); 3 List<Integer> lis

Problem Binary Tree Postorder Traversal

Problem Description: Given a binary tree, return the postorder traversal of its nodes' values Solution: 1 public List<Integer> postorderTraversal(TreeNode root) { 2 if (root == null) return new LinkedList<Integer>(); 3 List<Integer> list

Problem Binary Tree Maximum Path Sum

Problem Description: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. Solution: 1 public class Solution { 2 public int max; 3 public int max

HDU - 3644:A Chocolate Manufacturer&#39;s Problem(模拟退火, 求多边形内最大圆半径)

pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去.  问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那么简单.  因为我们的多边形可能是凹多边形,而前面的方法只对凸多边形有效. 学习了下模拟退火的算法,这个随机算法只在最小圆覆盖的时候写过. 这里再学一下,看起来更正宗一点的.  每次在当前点的附近(R)找是否能优化,而这个R慢慢变小,使得趋紧答案的趋势更精细. 判定点再多边形内:同样,不能用检验是否在每条

PKU Campus 2016 H: Magical Balls

H: Magical Balls 总Time Limit: 1000ms Memory Limit: 262144kB Description Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever. Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

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/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8861    Accepted Submission(s): 4317 Problem Description Many