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后处理器,可 ...
随机推荐
- Java-超市购物小票案例-详细介绍
1.1 超市购物购物小票需求分析 用户循环进行三个操作: 1.输入购买数量,代表为所购买货物的数量赋值,从而计算每项商品金额 2.打印小票,将已有数据打印 3.退出系统(因为该程序为循环操作,无法终 ...
- Oracle 表-初步涉水不深(第一天)
oracle 关系型数据库 注释:面对大型数据处理,市场占有率40%多(但是目前正往分布式转换) 故事:本来一台大型计算机才能处理的数据,美国科学家用100台家用电脑连接,成功处理了数据.. tabl ...
- CCF|分蛋糕|Java
import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...
- Android图片压缩,不失真,上线项目
当然了,图片压缩是利用了libjpeg库的基础上,牛逼的同学可以自行生成so.jar.在此给出一个链接: http://www.cnblogs.com/hrlnw/p/4403334.html 在生成 ...
- PC端浏览器定位
第一: PC端浏览器定位(纯前端)浏览器定位 :这里用了两种 ,一种是Html5自带的方法 另一种是引用了百度api ,百度api 的使用有三种:1 浏览器定位2 ip定位3 SDK辅助定位引用百度 ...
- 获取windows版本号
原文:https://blog.csdn.net/justFWD/article/details/44856277 内容整理如下,点击跳至指定内容: manifest文件加上compatibility ...
- vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改
vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改.vue <div class="commonHeader" v-bind:class=" ...
- php基础查找算法
1.顺序查找 function line_search($array,$tar) { if(!is_array($array) || count($array) < 1) return fals ...
- 诊断:AWR快照停止自动采集
11.2.0.4数据库中,MMON进程,有时候由于一些莫名其妙的原因挂掉,接下来AWR的快照也就无法正常自动生成.MMON进程应该自动重启,却并没有自动被启动. 那么我们有可能是遇到了bug Bug ...
- H5实现调用本地摄像头实现实时视频以及拍照功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...