Parent and son

本文介绍了一种在树形结构中进行高效查询的算法,针对特定的查询需求,如寻找子节点和后代节点中编号最小的节点,通过预处理和递归深度优先搜索实现了快速响应。算法首先初始化树的结构,然后进行深度优先遍历,记录每个节点的左右边界,最后通过这些信息快速回答查询。此算法适用于大规模树形数据结构的查询问题。
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*2];
int tot, sz, head[MAXN], L[MAXN], R[MAXN], s[MAXN], fa[MAXN];
void init()
{
    tot = 0;
    sz = 0;
    memset(head, -1, sizeof(head));
    memset(s, 0, 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 != -1; 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 = 0; i < n - 1; i++)
        {
            scanf("%d%d", &u, &v);
            s[u]++;
            s[v]++;
            addedge(u, v);
            addedge(v, u);
        }
        sz = 0;
        fa[1] = INF;
        dfs(1, 0);
        while (q--)
        {
            scanf("%d%d", &X, &Y);
            if (s[Y] == 1)
                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 = 1;
                if (Y == 1)
                {
                    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");
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7375201.html

with temp AS (SELECT mbs.manufacturing_bom_desc_id, mbs.parent_material_id, mbs.material_desc_id material_id, mbs.material_no, mbs.material_name, mbs.property material_property, mbs.company_id, mbs.station_code , mbs.station_name , mbs.station_id, sum(mbs.unit_dosage) as unit_dosage, mbs.colour, mbs.procedure, mbs.virtual_part_sign, mbs.bulk_sign, mbs.is_key_part FROM ${schema_basic}manufacturing_bom_son mbs WHERE mbs.manufacturing_bom_desc_id IN (SELECT id FROM ${schema_basic}manufacturing_bom_desc mbd WHERE mbd.is_used = 1 AND mbd.bom_status = 2 AND mbd.is_del = 0 <if test="param.materialNo != null and param.materialNo != ''"> and mbd.material_no = #{param.materialNo} </if> <if test="param.bomVersionNo != null and param.bomVersionNo != ''"> and mbd.bom_version_no = #{param.bomVersionNo} </if> <if test="param.erpOrderCode != null and param.erpOrderCode != ''"> and mbd.erp_order_code = #{param.erpOrderCode} </if> <if test="param.branchCompanyId != null and param.branchCompanyId != ''"> and mbd.branch_company_id = #{param.branchCompanyId} </if> <if test="param.stationCode != null and param.stationCode != ''"> and mbs.station_code = #{param.stationCode} </if> ) <if test="param.mark != null and param.mark != ''"> and mbs.bulk_sign is null and mbs.virtual_part_sign is null </if> group by mbs.manufacturing_bom_desc_id, mbs.parent_material_id, mbs.material_desc_id , mbs.material_no, mbs.material_name, mbs.property , mbs.company_id, mbs.station_code, mbs.station_name, mbs.station_id, mbs.colour, mbs.procedure, mbs.virtual_part_sign, mbs.bulk_sign, mbs.is_key_part ) SELECT temp.*, md.specs, md.model, md.base_unit_id, ud.unit_name FROM temp LEFT JOIN basic.material_desc md ON temp.material_id = md.id LEFT JOIN basic.unit_desc ud ON md.base_unit_id = ud.id 解读这段sql
07-04
通过短时倒谱(Cepstrogram)计算进行时-倒频分析研究(Matlab代码实现)内容概要:本文主要介绍了一项关于短时倒谱(Cepstrogram)计算在时-倒频分析中的研究,并提供了相应的Matlab代码实现。通过短时倒谱分析方法,能够有效提取信号在时间与倒频率域的特征,适用于语音、机械振动、生物医学等领域的信号处理与故障诊断。文中阐述了倒谱分析的基本原理、短时倒谱的计算流程及其在实际工程中的应用价值,展示了如何利用Matlab进行时-倒频图的可视化与分析,帮助研究人员深入理解非平稳信号的周期性成分与谐波结构。; 适合人群:具备一定信号处理基础,熟悉Matlab编程,从事电子信息、机械工程、生物医学或通信等相关领域科研工作的研究生、工程师及科研人员。; 使用场景及目标:①掌握倒谱分析与短时倒谱的基本理论及其与傅里叶变换的关系;②学习如何用Matlab实现Cepstrogram并应用于实际信号的周期性特征提取与故障诊断;③为语音识别、机械设备状态监测、振动信号分析等研究提供技术支持与方法参考; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,先理解倒谱的基本概念再逐步实现短时倒谱分析,注意参数设置如窗长、重叠率等对结果的影响,同时可将该方法与其他时频分析方法(如STFT、小波变换)进行对比,以提升对信号特征的理解能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值