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 ...
随机推荐
- Rsync数据远程同步备份
rsync的使用方法 一.设置Rsync Server端 Rsync server需要设定四个方面: 1.规划建立备份目录区 2.设定: /etc/xinetd.d/rsync 3.设定: /etc/ ...
- windows指令
& 无条件执行&符号后面的命令: && 当&&前面的命令成功执行时,执行&&后面的命令,否则不执行: || ...
- 用GDB调试程序的设置 Segmentation fault(Core Dump)调试
在写wifi库的时候碰见一个 Segmentation fault(Core Dump) 所以需要用GDB调试下. 在cmake的时候,修改CMakeLists.txt set(CMAKE_C_FLA ...
- Shell script fails: Syntax error: “(” unexpected
Shell script fails: Syntax error: “(” unexpected google 一下. http://unix.stackexchange.com/questions/ ...
- 转 : ANT 调用sqlplus 客户端
Jenkins安装与配置 (版本控制) 1 用ant脚本执行sql语句现在我需要写一个ant脚本来实现项目安装,情况是这样的,客户现在运行的版本可能是2.0,安装目录里可能有REL_1_0,REL_ ...
- 大学二三事——那些人(1)
校歌墙的对面是一座历史比较悠久的建筑,以前叫做12号楼,后来改成了"诚"字楼. 在诚字楼一楼昏暗的走廊上,你总是能看见一位大概四五十岁的大叔,有时他会指着挂在墙上的学校简介,一个人 ...
- PHP商品倒计时 php实现当前用户在线人数
//PHP商品倒计时 date_default_timezone_set("Asia/shanghai");//地区 //配置每天的活动时间段 $starttimestr = &q ...
- 新建aix实例
http://www.cnblogs.com/kfarvid/archive/2010/12/21/1912553.html DB2数据库 http://wenku.baidu.com/view/ ...
- lucene特殊字符处理
这是个郁闷的问题,今天遇到了,但在lucene中查询的关键字保护有特殊字符,譬如--,会出现如下异常: org.apache.lucene.queryParser.ParseException: Ca ...
- Oracle数据库设计小细节
1. 如果使用PowerDesigner此类工具,注意将工具的导出的SQL语句中对于表的双引号去掉. 2. 建表和建字段的时候,不同单词之间使用下划线分隔,比如 REC_ID 3. Oracle中数值 ...