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

若x在y的子树范围内,若y儿子最小值是x的前驱,从次小值与父亲节点转移,否则从最小值与父亲节点转移。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 100008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++) int dfn[N][2];
int fa[N];
int son[N][2], des[N];
struct Node{
int to,next;
}edge[N * 2]; int head[N],tot, deg[N];
int tim;
void init(){
memset(head, -1, sizeof(head));
memset(deg, 0, sizeof(deg));
tot = 0;
}
void add(int u, int to){
edge[tot].to=to;
edge[tot].next=head[u];
deg[u]++;
head[u]=tot++;
} void dfs(int u, int f){
fa[u] = f;
dfn[u][0] = tim++; son[u][0] = INF;
son[u][1] = INF;
des[u] = INF;
for(int i = head[u]; ~i ; i = edge[i].next){
int v = edge[i].to;
if(v != f){
dfs(v, u);
son[u][1] = min(son[u][1], v);
if(son[u][0] > son[u][1]){
swap(son[u][0], son[u][1]);
}
des[u] = min(des[u], v);
des[u] = min(des[u], des[v]);
}
}
dfn[u][1] = tim++;
} bool isFa(int x, int y){
if(dfn[x][0] <= dfn[y][0] && dfn[x][1] >= dfn[y][0]){
return true;
}
return false;
} int main(){
int t;
cin>>t;
while(t--){
int n, q;
scanf("%d %d", &n, &q);
init();
for(int i = 0; i < n - 1; i++){
int u, v;
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
tim = 0;
dfs(1, INF);//1号节点父亲注意要为NF,便于处理 int des2 = INF;//1号节点子树后继次小值
int minv = INF;//判断1号节点子树后继最小值从哪个儿子节点发出
for(int i = head[1]; ~i; i = edge[i].next){
int v = edge[i].to;
int tp = min(v, des[v]);
if(tp != des[1] && tp < des2){
des2 = tp;
}
if(tp == des[1]){
minv = v;
}
} while(q--){
int x, y;
scanf("%d %d", &x, &y);
if(deg[y] == 1){
printf("no answers!\n");
continue;
}
if(isFa(y, x)){//x在y子树范围内(以1为根时)
int mson, mdes;
if(isFa(son[y][0], x)){
mson = min(son[y][1], fa[y]);
}else{
mson = min(son[y][0], fa[y]);
} if(y != 1){
mdes = 1;
}else{
//y不是节点1,要判断
if(isFa(minv, x)){
mdes = des2;
}else{
mdes = des[y];
}
}
printf("%d %d\n", mson, mdes); }else{
//x不在y子树范围内(以1为根时),直接输出
printf("%d %d\n", son[y][0], des[y]);
}
}
printf("\n");
} return 0;
}

  

HDU4008 Parent and son(树形DP LCA)的更多相关文章

  1. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  2. hdu_5293_Tree chain problem(DFS序+树形DP+LCA)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293 被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写 ...

  3. poj 3417 树形dp+LCA

    思路:我以前一直喜欢用根号n分段的LCA.在这题上挂了,第一次发现这样的LCA被卡.果断改用Tarjan离线算法求LCA. 当前节点为u,其子节点为v.那么: 当以v根的子树中含有连接子树以外点的边数 ...

  4. POJ 3728 The merchant (树形DP+LCA)

    题目:https://vjudge.net/contest/323605#problem/E 题意:一棵n个点的树,然后有m个查询,每次查询找(u->v)路径上的两个数,a[i],a[j],(i ...

  5. Codeforces Round #343 (Div. 2) E. Famil Door and Roads (树形dp,lca)

    Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it ...

  6. HDU 4008 Parent and son

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

  7. P2495 [SDOI2011]消耗战 lca倍增+虚树+树形dp

    题目:给出n个点的树  q次询问  问切断 k个点(不和1号点联通)的最小代价是多少 思路:树形dp  sum[i]表示切断i的子树中需要切断的点的最小代价是多少 mi[i]表示1--i中的最小边权 ...

  8. BZOJ_2286_[Sdoi2011]消耗战_虚树+树形DP+树剖lca

    BZOJ_2286_[Sdoi2011]消耗战_虚树+树形DP Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的 ...

  9. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

随机推荐

  1. VB中 ByRef与ByVal区别

    函数调用的参数传递有"值传递"和"引用传递"两种传递方式.如果采用"值传递",在函数内部改变了参数的值,主调程序的对应变量的值不会改变:如果 ...

  2. How can I determine the URL that a local Git repository was originally cloned from?

    git remote show origin from: http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url- ...

  3. C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)

    数组是变量的索引列表,可以在方括号中指定索引来访问数组中的各个成员,其中索引是一个整数,从0开始. 一维数组 多维数组(矩形数组) 数组的数组(锯齿数组) 数组必须在访问之前初始化,数组的初始化有两种 ...

  4. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. Interleaving Positive and Negative Numbers

    Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...

  6. React 源码解读参考,理解原理。

    Rubix - ReactJS Powered Admin Template 文档:   http://rubix-docs.sketchpixy.com/ ===================== ...

  7. Bootstrap datepicker可配置网址

    http://eternicode.github.io/bootstrap-datepicker/?markup=input&format=&weekStart=&startD ...

  8. ACM/ICPC 之 四道MST-Kruskal解法-练习题(POJ1251-POJ1287-POJ2031-POJ2421)

    由于题目简单,部分题意写在代码中(简单题就应该多练英文...),且较少给注释,需要注意的地方会写在代码中,虽然四个题意各有千秋,但万变不离其宗,细细思考一番会发现四道题都属于很直接的最小生成树问题,由 ...

  9. codeforces 492C. Vanya and Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n,  r,  avg.然后有 n 行,每行有两个数:第 i 行有 ...

  10. 【leetcode】Max Points on a Line(hard)☆

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...