Give you a tree with N vertices and N‐ 1 edges, and then ask you Q queries on “which vertex is Y's son that has the smallest number and which vertex is Y’s descendants that has the smallest number if we choose X as the root of the entire tree?”

InputThe first line of input is an integer T (T<=10) means the case number. 
The first line of each test case contains N(2 ≤ N ≤ 100,000) and Q(1 ≤ Q ≤ 100,000). 
Each of the following N ‐ 1 lines of the test case contains two integers a(1 ≤ a ≤ N) and b(1 ≤ b ≤ N) indicating an edge between a and b. 
Each of the following Q lines of the test case contains two integers X(1 ≤ X ≤ N) and Y(1 ≤ Y ≤ N, Y ≠ X) indicating an query. 
OutputFor each query, output the Y's son which has the smallest number and Y's descendant that has the smallest number if X is the root of the entire tree. If Y has no sons then output “no answers!”. There is an empty line after each case.Sample Input

1
7 3
1 2
1 5
2 3
2 4
5 6
5 7
1 2
5 3
3 2

Sample Output

3 3
no answers!
1 1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; #define MAXN 100006
#define INF 0x3f3f3f3f struct edge
{
int to, next;
}E[MAXN*];
int tot, sz, head[MAXN], L[MAXN], R[MAXN], s[MAXN], fa[MAXN];
void init()
{
tot = ;
sz = ;
memset(head, -, sizeof(head));
memset(s, , sizeof(s));
}
void addedge(int f, int t)
{
E[tot].to = t;
E[tot].next = head[f];
head[f] = tot++;
}
struct node
{
int min1, min2, id1, id2;
}sn[MAXN],st[MAXN]; void getmin(node& tmp, int num, int v)
{
if (tmp.min1 > num)
{
tmp.min2 = tmp.min1, tmp.id2 = tmp.id1;
tmp.min1 = num, tmp.id1 = v;
}
else if (tmp.min2 > num)
{
tmp.min2 = num, tmp.id2 = v;
}
} void dfs(int u, int pre)
{
L[u] = ++sz;
sn[u].min1 = sn[u].min2 = st[u].min1 = st[u].min2 = INF;
for (int i = head[u]; i != -; i = E[i].next)
{
int v = E[i].to;
if (v == pre) continue;
dfs(v, u);
fa[v] = u;
getmin(sn[u], v, v);
getmin(st[u], min(st[v].min1, v), v);
}
R[u] = sz;
}
int main()
{
int T, n, u, v, q, X, Y;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &q);
init();
for (int i = ; i < n - ; i++)
{
scanf("%d%d", &u, &v);
s[u]++;
s[v]++;
addedge(u, v);
addedge(v, u);
}
sz = ;
fa[] = INF;
dfs(, );
while (q--)
{
scanf("%d%d", &X, &Y);
if (s[Y] == )
printf("no answers!\n");
else if (L[X]<L[Y] || R[X]>R[Y])
printf("%d %d\n", sn[Y].min1, st[Y].min1);
else
{
int ans = ;
if (Y == )
{
if (L[st[Y].id1] <= L[X] && R[st[Y].id1] >= R[X])
{
ans = st[Y].min2;
}
else
ans = st[Y].min1;
}
if (L[sn[Y].id1] <= L[X] && R[sn[Y].id1] >= R[X])
{
printf("%d %d\n", min(fa[Y], sn[Y].min2), ans);
}
else
printf("%d %d\n", min(fa[Y], sn[Y].min1), ans);
}
}
printf("\n");
}
}

Parent and son的更多相关文章

  1. HDU4008 Parent and son(树形DP LCA)

    先记录以1为根时每个节点子树儿子节点的最大与次小值,询问x, y时,先判断x在不在y的子树范围内,若不在,结果为y的儿子结点,后继的最小值. 若x在y的子树范围内,若y儿子最小值是x的前驱,从次小值与 ...

  2. HDU 4008 Parent and son LCA+树形dp

    题意: 给定case数 给定n个点的树,m个询问 以下n-1行给出树边 m个询问 x y 问:以x为根.y子树下 y的最小点标的儿子节点 和子孙节点 思路: 用son[u][0] 表示u的最小儿子 s ...

  3. HDU 4008 Parent and son

    树形DP+LCA+思路.这题可真是有点难度......所以准备详细写一下题解. 题意:给一颗无根树,有Q次询问,每次询问指定一个根节点X,然后让你计算Y节点的儿子和子孙中,编号最小的节点是多少. 我们 ...

  4. [c++] Smart Pointers

    内存管理方面的知识 基础实例: #include <iostream> #include <stack> #include <memory> using names ...

  5. Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点

    开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...

  6. python 学习笔记二十 django项目bbs论坛

    项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...

  7. Java(接口与继承)动手动脑

    1>继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改 Parent 构造方法的代码,显式调用 GrandParen ...

  8. JS魔法堂:判断节点位置关系

    一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二 ...

  9. 前端构建:Less入了个门

    一.前言   说到前端构建怎能缺少CSS预处理器呢!其实CSS的预处理器有很多啦,比较出名的有Scss.Sass.Stylus和Less.(最近还听说出现了Autoprefixer等CSS后处理器,可 ...

随机推荐

  1. Ref 和 Out 区别(演练代码)

    一.代码 今天就总结Ref和Out 的总结,这东西,也是经常面试过程中,笔试经常考的,比如:请简述Ref和Out 的区别,或者通过一段代码让你计算这过程的结果.... Out代码实例::: stati ...

  2. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FileSize"

    请求阿里云的OSS接口图片信息,返回json格式的数据,通过ObjectMapper将json转为Image对象时候报错转换失败 将json转对象的代码: String jsonStr = " ...

  3. R in action读书笔记(7)-第七章:基本统计分析(下)

    7.3相关 相关系数可以用来描述定量变量之间的关系.相关系数的符号(±)表明关系的方向(正相关或负相关),其值的大小表示关系的强弱程度(完全不相关时为0,完全相关时为1).除了基础安装以外,我们还将使 ...

  4. pringBoot Controller接收参数的几种常用方式

    第一类:请求路径参数1.@PathVariable 获取路径参数.即url/{id}这种形式.2.@RequestParam 获取查询参数.即url?name=这种形式例子 GEThttp://loc ...

  5. USB设备请求命令详解

    USB设备请求命令 :bmRequestType + bRequest + wValue + wIndex + wLength 编号 值  名称 (0) 0  GET_STATUS:用来返回特定接收者 ...

  6. jq ajax请求error: Maximum call stack size exceeded

    原因是data中参数iconUrl这个变量未声明导致的.jq在内部循环时报错

  7. python视频 神经网络 Tensorflow

    python视频 神经网络 Tensorflow 模块 视频教程 (带源码) 所属网站分类: 资源下载 > python视频教程 作者:smile 链接:http://www.pythonhei ...

  8. Python中的列表(6)

    列表切片 如何拿到列表中的部分元素,Python 引入了 “切片” 的概念. 上代码: words = ['a','b','c','d'] print(words[0:3]) console: 冒号( ...

  9. 将文件大小kb转换成M

    得到文件的大小的一般是直接到得到的是文件的字节大小,也就是kb,我们有的时候需要做单位换算成B或者M, 下面方法只是换成M,没有到G, 有更好的方法,请随时沟通,以便交流学习,谢谢. public s ...

  10. 又一个ajax实例,结合jQuery

    又一个ajax实例,配合jQuery   html <!DOCTYPE html> <html lang="zh-cn"> <head> < ...