Parent and son
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的更多相关文章
- HDU4008 Parent and son(树形DP LCA)
先记录以1为根时每个节点子树儿子节点的最大与次小值,询问x, y时,先判断x在不在y的子树范围内,若不在,结果为y的儿子结点,后继的最小值. 若x在y的子树范围内,若y儿子最小值是x的前驱,从次小值与 ...
- HDU 4008 Parent and son LCA+树形dp
题意: 给定case数 给定n个点的树,m个询问 以下n-1行给出树边 m个询问 x y 问:以x为根.y子树下 y的最小点标的儿子节点 和子孙节点 思路: 用son[u][0] 表示u的最小儿子 s ...
- HDU 4008 Parent and son
树形DP+LCA+思路.这题可真是有点难度......所以准备详细写一下题解. 题意:给一颗无根树,有Q次询问,每次询问指定一个根节点X,然后让你计算Y节点的儿子和子孙中,编号最小的节点是多少. 我们 ...
- [c++] Smart Pointers
内存管理方面的知识 基础实例: #include <iostream> #include <stack> #include <memory> using names ...
- Python之路【第十八篇】Django小项目简单BBS论坛部分内容知识点
开发一个简单的BBS论坛 项目需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可被 ...
- python 学习笔记二十 django项目bbs论坛
项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...
- Java(接口与继承)动手动脑
1>继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改 Parent 构造方法的代码,显式调用 GrandParen ...
- JS魔法堂:判断节点位置关系
一.前言 在polyfill querySelectorAll 和写弹出窗时都需要判断两个节点间的位置关系,通过jQuery我们可以轻松搞定,但原生JS呢?下面我将整理各种判断方法,以供日后查阅. 二 ...
- 前端构建:Less入了个门
一.前言 说到前端构建怎能缺少CSS预处理器呢!其实CSS的预处理器有很多啦,比较出名的有Scss.Sass.Stylus和Less.(最近还听说出现了Autoprefixer等CSS后处理器,可 ...
随机推荐
- c#拖拽文件
在“属性”窗口中,先设置MDI的父窗口的AllowDrop 属性更改为true;2.在父窗口的事件中添加下面两个事件 private void Form1_DragEnter(object sende ...
- jQueryUI 购物车拖放功能
<style type="text/css"> .basket{ border:transparent solid 2px; } img{ width:80px; he ...
- 一个iOS开发者的修真之路
在微信上有童鞋问我iOS开发者的入门标准是神马?这个问题难到我了,而且贸然给一个答案出来的话,必定会有万千高手来喷. 凡人修仙,仙人修道,道人修真.当我们还是一个在青石板上蹲马步汗水涔涔的废柴时,或许 ...
- linux命令useradd添加用户
linux命令useradd添加用户详解 1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户. 2.格式 useradd [-d home] [-s ...
- CREATE OPERATOR CLASS - 定义一个新的操作符类
SYNOPSIS CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type USING index_method AS { OPERATOR ...
- ascii - 在八进制,十进制,十六进制中的 ASCII 字符集编码
描述 ASCII 是美国对于信息交换的标准代码,它是7位码,许多8位码(比如 ISO 8859-1, Linux 的默认字符集)容纳 ASCII 作为它们的下半部分.对应的国际 ASSII 是 ISO ...
- 取消input聚焦时的边框,去除ios点击时,自动添加的底色效果
/*去除ios点击时,自动添加的底色效果*/ -webkit-tap-highlight-color: rgba(, , , ); /*去除焦点框*/ outline:none;
- php简单实用的调试工具类
<?php /* * 调试类 */ class Common_Debug { //打开错误报告 public static function showError($debug = true) { ...
- tensorflow ConfigProto
tf.ConfigProto一般用在创建session的时候.用来对session进行参数配置 with tf.Session(config = tf.ConfigProto(...),...)#tf ...
- MYSQL有那些优化?
版权声明:本文为博主转载文章,原博主地址: https://blog.csdn.net/u013087513/article/details/77899412 MySQL优化三大方向 ① 优化MySQ ...