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. Activiti工作流和shiro权限管理关系图

  2. linux下mysql开启可访问

    修改mysql配置连接信息 将bind-address注释 vim /etc/my.cnf 修改mysql用户授权 mysql>GRANT ALL PRIVILEGES ON *.* TO ' ...

  3. spring 中文乱码问题

    spring 开发过程中的中文乱码问题主要分为以下几种: 1.前端传参数到后台前  就已经乱码. 这个很大原因就是前端的问题了! 2.传入后台后,乱码. 可能存在几个原因: 2.1 传入tomcat前 ...

  4. uoj #15. 【NOIP2014】生活大爆炸版石头剪刀布

    石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一 样,则不分胜负.在<生活大爆炸>第二季第 8 集中出现了一种石头剪刀布的升级版游戏. 升级版游戏在传统的石头剪 ...

  5. 我用的主机,推荐给大家【gegehost】【戈戈主机】

    炎炎夏日冰点价格:戈戈主机史上最大优惠促销活动 1.7月1日至8日:买主机优惠大促销:主机买一送一,不限购买数量 请您通过客户中心或者淘宝购买一个主机之后,登录客户中心,提交问题, 提供要赠送的主机的 ...

  6. IT实习总结-上海的实习的日子

    在上海实习两个多月,从找工作,到面试,最后到创业公司工作,体会了一个人在外工作的那种工作时的压力与双休时的寂寞,有时甚至有点迷惘,可能这就是生活,每个人都会经历,在经历的过程中会有不同的味道. 说说我 ...

  7. createuser - 定义一个新的 PostgreSQL 用户帐户

    SYNOPSIS createuser [ option...] [ username] DESCRIPTION 描述 createuser 创建一个新的 PostgreSQL 用户.只有超级用户(在 ...

  8. 拼字符串 showArray.push(data); showArray.join(",")

    //---// var showColumns = getShowColsRows("table");// var showArray = [];// $.each(showCol ...

  9. JavaScipt30(第二十二个案例)(主要知识点:getBoundingClientRect)

    这是第二十二个案例,这个例子实现的是鼠标移入a标签时,将其高亮. 附上项目链接: https://github.com/wesbos/JavaScript30 以下为注释后的源码: <scrip ...

  10. OpenMP用法大全

    OpenMP基本概念OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的并 ...