Tree 2

Time limit: 1.0 second
Memory limit: 64 MB
Consider a tree consisting of n vertices. A distance between two vertices is the minimal number of edges in a path connecting them. Given a vertex vi and distance di find a vertex ui such that distance between vi and ui equals to di.

Input

The first line contains the number of vertices n (1 ≤ n ≤ 20000) and the number of queries q(1 ≤ q ≤ 50000). Each of the following n − 1 lines describes an edge and contains the numbers of vertices connected by this edge. Vertices are numbered from 1 to n. The next q lines describe the queries. Each query is described by a line containing two numbers vi (1 ≤ vi ≤ n) and di(0 ≤ di ≤ n).

Output

You should output q lines. The i-th line should contain a vertex number ui, the answer to the i-th query. If there are several possible answers, output any of them. If there are no required vertices, output 0 instead.

Sample

input output
9 10
1 8
1 5
1 4
2 7
2 5
3 6
5 9
6 9
5 4
8 1
4 3
2 4
9 3
1 1
5 2
3 5
6 4
7 3
0
1
2
3
4
5
6
7
8
9

分析:参考自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的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. blob的存储与读取

    对于oracle数据库的blob的存储与读取对应的是byte数组. 将blob类型数据存入数据库: String blob: byte[] byte = blob.getBytes(); entity ...

  2. 关于IP在MySQL中的存储

    对于很多新手而言,他们总会纠结,怎样才能更好的设计MySQL数据库呢:作为一个从菜鸟走过来的人,深有体会,刚开始我也不知道什么是外键.什么是事务处理.怎样合理的定义一个字段,说到字段,今天我就带领大家 ...

  3. 关于Select * 与Select 字段名 的问题!

    [转]http://blog.csdn.net/tongyu2009/article/details/8252418 1.SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否 ...

  4. Phonegap解决错误:Error initializing Cordova:Class not found

    Phonegap  解决错误: Alert [ERROR]Error initializing Cordova:Class not found 发现bug后找原因   网上说是  因为找不到     ...

  5. STM32F407的硬件I2C

    源:STM32F407的硬件I2C 我使用的是STM32的固件库. 硬件模块使用之前必须配置其参数,I2C的配置如下: void IIC_Config(void) { GPIO_InitTypeDef ...

  6. FreeRTOS中断优先级配置(重要)

    FreeRTOS中断优先级配置(重要) 本章节为大家讲解FreeRTOS中断优先级配置,此章节非常重要,初学者经常在这里犯迷糊.对于初学者来说,本章节务必要整明白.12.1 NVIC基础知识12.2  ...

  7. adodb.stream对象的方法/属性

    Cancel 方法      使用方法如下      Object.Cancel      说明:取消执行挂起的异步 Execute 或 Open 方法的调用.Close   方法      使用方法 ...

  8. MVC 和 MVVM

    MVVM MVVM 是 Model-View-ViewModel 的简写,MVVM 模式和 MVC 模式一样,主要目的是分离视图(View)和模型(Model) MVC 回顾 MVC 结构图 MVC ...

  9. 从头到尾彻底解析Hash表算法

    作者:July.wuliming.pkuoliver 说明:本文分为三部分内容, 第一部分为一道百度面试题Top K算法的详解:第二部分为关于Hash表算法的详细阐述:第三部分为打造一个最快的Hash ...

  10. Android OpenGL ES(十三)通用的矩阵变换指令 .

    Android OpenGL ES 对于不同坐标系下坐标变换,大都使用矩阵运算的方法来定义和实现的.这里介绍对应指定的坐标系(比如viewmodel, projection或是viewport) An ...