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. java和.net的类比

    原文地址在http://www.seguetech.com/blog/2013/06/03/dotnet-vs-java-how-to-pick

  2. vs找不到svn源代码管理插件之我见

    使用svn要安装两个文件,一个客户端:TortoiseSVN-1.8.msi,一个插件:AnkhSvn-2.5.msi:两个都安装好之后,在vs的tool(工具)选项卡中,选择自定义,然后选择sour ...

  3. linux下编译安装apache

    在linux(CentOS6.5)上安装Apache,要首先确保以下程序事先安装 apr:The mission of the Apache Portable Runtime (APR) projec ...

  4. photoshop移动工具

    1*移动工具 V  移动图层  若果移动选区相当于剪切 2*

  5. Object调用控件的办法

    <OBJECT id="pwdpad" style="LEFT: 0px; WIDTH: 35px; TOP: 0px; HEIGHT: 16px" cl ...

  6. C# 截取字符串某个字符分割的最后一部分

    例如 string s1="123.456.789",想截取得到的新字符串为“789” 代码如下: string s1 = "123.456.789"; str ...

  7. event小解

    首先是一个小例子: <input type="text" onclick="a(event)"/> function a(event){   con ...

  8. 使用Core Animation对象来实现动画

    转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...

  9. MVVM 入门介绍

    转载自:http://www.objccn.io/issue-13-1/ 我于 2011 年在 500px 找到自己的第一份 iOS 开发工作.虽然我已经在大学里做了好几年 iOS 外包开发,但这才是 ...

  10. [转]View属性 之 paddingStart & paddingEnd

    [CAUSE] 在写一个自定义View时, 直接复制了Android-Source的XML布局文件, 默认开发SDK版本是4.2.2(Level-API-17), 后因其他原因将SDK版本改为4.1. ...