Problem Statement

You are given a tree with $N$ vertices. The vertices are numbered $1, \dots, N$, and the $i$-th ($1 \leq i \leq N - 1$) edge connects Vertices $A_i$ and $B_i$.

We define the distance between Vertices $u$ and $v$ on this tree by the number of edges in the shortest path from Vertex $u$ to Vertex $v$.

You are given $Q$ queries. In the $i$-th ($1 \leq i \leq Q$) query, given integers $U_i$ and $K_i$, print the index of any vertex whose distance from Vertex $U_i$ is exactly $K_i$. If there is no such vertex, print -1.

Constraints

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq N - 1)$
  • The given graph is a tree.
  • $1 \leq Q \leq 2 \times 10^5$
  • $1 \leq U_i, K_i \leq N \, (1 \leq i \leq Q)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $B_1$
$\vdots$
$A_{N-1}$ $B_{N-1}$
$Q$
$U_1$ $K_1$
$\vdots$
$U_Q$ $K_Q$

Output

Print $Q$ lines. The $i$-th ($1 \leq i \leq Q$) line should contain the index of any vertex whose distance from Vertex $U_i$ is exactly $K_i$ if such a vertex exists; if not, it should contain -1. If there are multiple such vertices, you may print any of them.


Sample Input 1

5
1 2
2 3
3 4
3 5
3
2 2
5 3
3 3

Sample Output 1

4
1
-1
  • Two vertices, Vertices $4$ and $5$, have a distance exactly $2$ from Vertex $2$.
  • Only Vertex $1$ has a distance exactly $3$ from Vertex $5$.
  • No vertex has a distance exactly $3$ from Vertex $3$.

Sample Input 2

10
1 2
2 3
3 5
2 8
3 4
4 6
4 9
5 7
9 10
5
1 1
2 2
3 3
4 4
5 5

Sample Output 2

2
4
10
-1
-1

对于一个点 \(u\),如果我们找到离他最远的点 \(v\),那么此时如果 \((u,v)\) 的距离还超不过 \(k\),肯定无解。否则,就在路径 \((u,v)\) 上寻找到那个离 \(u\) 刚好是 \(k\) 格的点就行了。

但是最远的点在哪里呢?有一个定理,就是一棵树上,离一个点最远的点一定是直径的两端点之一。

所以求出原来直径的两个端点 \((u',v')\),然后以他们两个分别为根,跑出一次倍增。询问的时候就可以看两个端点哪个离他更远,然后在对应的倍增数组上找到离他恰好为 \(k\) 的点。复杂度 \(O(nlogn+qlogn)\)

当然也可以用长剖卡到 \(O(nlogn+q)\),不过没必要。

#include<cstdio>
const int N=2e5+5;
int n,u,v,rt,fa[N][23],dp1[N],dp2[N],f[N][23],ans(-1),hd[N],e_num,q,k;
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
void dfs(int x,int y,int dep)
{
if(dep>ans)
ans=dep,rt=x;
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v!=y)
dfs(e[i].v,x,dep+1);
}
void sou(int x,int y)
{
dp1[x]=dp1[y]+1,f[x][0]=y;
if(dp1[x]>ans)
ans=dp1[x],rt=x;
for(int i=1;i<20;i++)
f[x][i]=f[f[x][i-1]][i-1];
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v!=y)
sou(e[i].v,x);
}
void suo(int x,int y)
{
dp2[x]=dp2[y]+1,fa[x][0]=y;
// printf("%d %d\n",x ,dp2[x]);
for(int i=1;i<20;i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v!=y)
suo(e[i].v,x);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1,0,0);
ans=0;
sou(rt,ans=0);
suo(rt,0);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&u,&k);
if(dp1[u]>k)
{
for(int i=0;i<20;i++)
if(k>>i&1)
u=f[u][i];
printf("%d\n",u);
}
else if(dp2[u]>k)
{
for(int i=0;i<20;i++)
if(k>>i&1)
u=fa[u][i];
printf("%d\n",u);
}
else
puts("-1");
}
}

[ABC267F] Exactly K Steps的更多相关文章

  1. 字典序的第K小数字

    今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...

  2. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  3. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  5. 35.两链表的第一个公共结点[Find the first common node of two linked list]

    [题目] 两个单向链表,找出它们的第一个公共结点. 链表的结点定义为:  C++ Code  123456   struct ListNode {     int         m_nKey;    ...

  6. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  7. 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  8. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  9. leetcode 189

    189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and ...

  10. poj2486Apple Tree[树形背包!!!]

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9989   Accepted: 3324 Descri ...

随机推荐

  1. [ABC126E] 1 or 2

    2023-01-07 题目 题目传送门 翻译 翻译 难度&重要性(1~10):2 题目来源 AtCoder 题目算法 并查集 解题思路 因为每张卡片上的数字只能是 \(1\) 或者 \(2\) ...

  2. Go 并发编程 - 并发安全(二)

    什么是并发安全 并发情况下,多个线程或协程会同时操作同一个资源,例如变量.数据结构.文件等.如果不保证并发安全,就可能导致数据竞争.脏读.脏写.死锁.活锁.饥饿等一系列并发问题,产生重大的安全隐患,比 ...

  3. selenium-wire兼容selenium和requests

    背景 在工作中UI自动化中可能会需要用到API来做一些数据准备或清理的事情,那UI操作是略低效的,但API操作相对高效. 而实战课就有这样一个案例,不过那个案例是UI操作和API分开的. 极少会遇到这 ...

  4. RabbitMQ 如何实现延迟队列?

    延迟队列是指当消息被发送以后,并不是立即执行,而是等待特定的时间后,消费者才会执行该消息. 延迟队列的使用场景有以下几种: 未按时支付的订单,30 分钟过期之后取消订单. 给活跃度比较低的用户间隔 N ...

  5. 超全技术学习资料PDF分享

    技术学习资料分享,目前共20G,持续更新... Java学习资料: 大数据Hadoop: 这里不一一截图了,资源持续更新中. 关注下面公众号进行下载.

  6. 使用 Sealos 一键部署高可用 MinIO,开启对象存储之旅

    大家好!今天这篇文章主要向大家介绍如何通过 Sealos 一键部署高可用 MinIO 集群. MinIO 对象存储是什么? 对象是二进制数据,例如图像.音频文件.电子表格甚至二进制可执行代码.对象的大 ...

  7. 什么是 x10 开发工具?「GitHub 热点速览」

    都听过 10x 工程师,一个人顶得过十个人.但是并不是每个人都是 10x 工程师,但是有些效率工具可能让你变成 2x.3x 的工程师.比如,这周火爆的 3D 游戏引擎 FlaxEngine 有着强大的 ...

  8. Mysql忘记密码后如何重置密码

    长时间不使用本机的Mysql后把密码忘记了咋整?直接上干货: 第一步(Mysql部署的位置,若自己能找到就忽略这一步):任务管理器中也可以找到 第二步:修改配置文件 在my.ini末尾加上 skip- ...

  9. Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773)

    Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773) 复现环境 采用Vulfocus靶场环境进行复 ...

  10. 3DMatch

    详细描述链接:3DMatch 数据集.github(介绍非常详细) 官网主页: 主页 3DMatch数据集收集了来自于62个场景的数据,其中54个场景的数据用于训练,8个场景的数据用于评估,其具体名称 ...