ural1752 Tree 2
Tree 2
Memory limit: 64 MB
Input
Output
Sample
| input | output |
|---|---|
9 10 |
0 |
分析:参考自http://www.lai18.com/content/7044719.html;
首先找到树的直径的两个端点,因为如果在端点到询问点之间没有答案,那么肯定没有答案;
然后根据端点建树,若存在答案,则需要求出距离询问点为d的点,而这个点就在树根与询问点之间且距询问点为d;
然后关键的地方就是对每个点,保留距离为2^k的祖先,存放在祖先数组中,这样就可以利用倍增求出答案了;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000000
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e4+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,s,q,dis[maxn][],fa[maxn][],anc[maxn][][],ma,id;
vi a[maxn];
void dfs(int now,int pre)
{
for(int x:a[now])
{
if(x!=pre)
{
dis[x][]=dis[now][]+;
if(dis[x][]>ma)ma=dis[x][],id=x;
dfs(x,now);
}
}
}
void dfs1(int now,int pre,int p)
{
for(int x:a[now])
{
if(x!=pre)
{
dis[x][p]=dis[now][p]+;
fa[x][p]=now;
dfs1(x,now,p);
}
}
}
void init()
{
memset(anc,-,sizeof anc);
for(int k=;k<;k++)
{
for(int i=;i<=n;i++)
{
anc[i][][k]=fa[i][k];
}
for(int j=;(<<j)<n;j++)
{
for(int i=;i<=n;i++)
{
if(anc[i][j-][k]!=-)
{
anc[i][j][k]=anc[anc[i][j-][k]][j-][k];
}
}
}
}
}
int query(int p,int now,int d)
{
for(int i=;i>=;i--)
{
if(d>=(1LL<<i))
{
d-=(1LL<<i);
now=anc[now][i][p];
}
if(d==)return now;
}
}
int main()
{
int i,j;
id=;
scanf("%d%d",&n,&q);
rep(i,,n-)scanf("%d%d",&j,&k),a[j].pb(k),a[k].pb(j);
dfs(,-);s=id;
ma=;id=;
memset(dis,,sizeof dis);
dfs(s,-);t=id;
memset(dis,,sizeof dis);
dfs1(s,-,);dfs1(t,-,);
init();
while(q--)
{
int u,d;
scanf("%d%d",&u,&d);
if(dis[u][]>=d)printf("%d\n",query(,u,d));
else if(dis[u][]>=d)printf("%d\n",query(,u,d));
else puts("");
}
//system("Pause");
return ;
}
ural1752 Tree 2的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- caffe卷积输入通道如何到输出通道
今天一个同学问 卷积过程好像是对 一个通道的图像进行卷积, 比如10个卷积核,得到10个feature map, 那么输入图像为RGB三个通道呢,输出就为 30个feature map 吗, 答案肯定 ...
- js1中call和apply的用法
js1中call和apply的用法 е辊顷 饼蹭瑭 岚辗疥 碜坪命 笛攮鼠 鲳篝等 ざ遛膜 镀鞭冢蒯 晕 册薷濑 就不是抓了而是人拳啪啪两声两个人都被拳头打在了腿骨 许郾犍 国 ...
- c++11 对象池的实现
; template <typename T> class ObjectPool { template <typename... Args> using Constructor ...
- 学习笔记:GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2)
GLSL Core Tutorial – Pipeline (OpenGL 3.2 – OpenGL 4.2) GLSL 是一种管道,一种图形化的流水线 1.GLSL 的具体工作流程: 简化流程如下: ...
- Got a packet bigger than ‘max_allowed_packet’ bytes的解决方法
在使用 longtext 类型执行数据录入时,有时会抛出这个异常,从字面理解就是当前包大小超过 mysql 系统设置的包大小无法执行操作. 解释一下包大小这个东西:简单来说就是mysql把当前执行的m ...
- 快学Scala-第八章 继承
知识点: 1.扩展类 extends关键字,在定义中给出子类需要而超类没有的字段和方法,或者重写超类的方法. 2.重写方法 在Scala中重写一个非抽象方法必须 override 修饰符 public ...
- makefile的编写规则
2. 编写makefile 示例: test:main.o func.o gcc -o test main.o func.o func.o:func.c gcc -c func.c mai ...
- ExtJS4 的dom
Ext使用了三个核心的工具类对我们掌握的DOM进行了完美的封装. ┣ Ext.Element(几乎对DOM的一切进行了封彻底装) ┣ Ext.DomHelper(一个强大的操控UI界面的工具类) ┣ ...
- android 5.1 API简介
android 5.1介绍: http://developer.android.com/about/versions/android-5.1.html?utm_campaign=lollipop-51 ...
- 如何让Spring MVC接收的参数可以转换为java对象
场景: web.xml中增加了一个DispatcherServlet配置,并在同级目录下添加了**-servlert.xml文件,搭建起了一个spring mvc的restful访问接口. 问题描述: ...