Query on a tree II

Query on a tree II

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.
We will ask you to perfrom some instructions of the following form:
DIST a b : ask for the distance between node a and node b
KTH a b k : ask for the k-th node on the path from node a to node b
Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:
In the first line there is an integer N (N <= 10000)
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
The next lines contain instructions "DIST a b" or "KTH a b k"
The end of each test case is signified by the string "DONE".
There is one blank line between successive tests.

For each "DIST" or "KTH" operation, write one integer representing its result.
Print one blank line after each test.

Input:

1
6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:

5
3
```
树上倍增即可
同时倍增父亲和距离
注意计算中的+1 -1

#include <bits/stdc++.h>
using namespace std;
#define maxn (int)(1e5+10)
#define LL long long

inline int read(){
    int rtn=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))rtn=(rtn<<1)+(rtn<<3)+ch-'0',ch=getchar();
    return rtn*f;
}

struct node{
    int a,b,nt,w;
}e[maxn];

LL w[maxn][21];
int fa[maxn][21],dep[maxn],p[maxn],cnt;

inline void add(int x,int y,int z){
    e[++cnt].a=x;e[cnt].b=y;e[cnt].w=z;
    e[cnt].nt=p[x];p[x]=cnt;
}

inline void dfs(int k){
    for(int i=1;i<=20;i++)fa[k][i]=fa[fa[k][i-1]][i-1];
    for(int i=1;i<=20;i++)w[k][i]=w[fa[k][i-1]][i-1]+w[k][i-1];
    for(int i=p[k];i;i=e[i].nt){
        int kk=e[i].b;
        if(kk==fa[k][0])continue;
        fa[kk][0]=k;dep[kk]=dep[k]+1;w[kk][0]=e[i].w;
        dfs(kk);
    }
}

inline int lca(int x,int y){
    if(dep[x]<dep[y])swap(x,y);
    for(int i=20;i>=0;i--){
        if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
    } 
    if(x==y)return y;
    for(int i=20;i>=0;i--){
        if(fa[x][i]!=fa[y][i])
            x=fa[x][i],y=fa[y][i];
    }return fa[x][0];
}

inline LL dis(int x,int y){
    LL rtn=0;
    int l=lca(x,y);
    for(int i=20;i>=0;i--){
        if(dep[fa[x][i]]>=dep[l])
            rtn+=w[x][i],x=fa[x][i];
    }
    for(int i=20;i>=0;i--){
        if(dep[fa[y][i]]>=dep[l])
            rtn+=w[y][i],y=fa[y][i];
    }return rtn;
}

inline int kth(int x,int y,int k){
    int l=lca(x,y);
    if(k<=dep[x]-dep[l]){
        k-=1;
        for(int i=20;i>=0;i--){
            if(1<<i<=k)x=fa[x][i],k-=1<<i;
        }
        return x;
    }
    else if(k>dep[x]-dep[l]){
        k=(dep[y]-dep[l]-(k-(dep[x]-dep[l])))+1;
        for(int i=20;i>=0;i--){
            if(1<<i<=k)y=fa[y][i],k-=1<<i;
        }
        return y;
    }
}

int main(){
    int T=read();
    while(T--){
        int n=read();cnt=0;
        memset(p,0,sizeof(p));
        memset(w,0,sizeof(w));
        memset(fa,0,sizeof(fa));
        for(int i=1;i<n;i++){
            int a=read(),b=read(),w=read();
            add(a,b,w);add(b,a,w);
        }
        dfs(1);
        while(true){
            char ch[10];scanf("%s",ch);
            if(ch[1]=='O')break;
            else if(ch[1]=='T'){
                int x=read(),y=read(),k=read();
                printf("%d\n",kth(x,y,k));
            }
            else if(ch[1]=='I'){
                int x=read(),y=read();
                printf("%lld\n",dis(x,y));
            }
        }
    }
    return 0;
} 

转载于:https://www.cnblogs.com/DexterYsw/p/7954838.html

欧姆龙PMAC CK3M控制器新手避坑指南:从开箱到EtherCAT总线配置的完整流程 本文详细介绍了欧姆龙PMAC CK3M控制器的完整配置流程,从开箱检查到EtherCAT总线配置,特别针对新手常见问题提供避坑指南。内容包括硬件连接、软件安装、EtherCAT主站初始化、从站配置、电机参数设置等关键步骤,帮助工程师快速掌握PMAC控制器的使用技巧。 阅读详情

相关推荐

Dynamics 365 F&O 开发入门(一)-- 从零开始建表

从零开始一步一步教你在Dynamics 365 F&O中创建一个表

qq_48843110的博客 942

【SPOJ913】Query on a tree II

题意: 给定一棵n个点的树,边具有边权。要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾。   解析:        树上倍增。   代码:   #include &lt;bits/stdc++.h&gt; using namespace std; cons...

ETO的博客 233

超越界面设计:GUI Guider与LVGL在STM32F103上的系统集成与架构思考

本文探讨了GUI Guider与LVGL在STM32F103微控制器上的系统集成与架构设计,重点分析了工程模块化组织、内存管理优化、硬件驱动接口设计以及FreeRTOS多任务集成。针对STM32F103ZET6的资源限制,提供了实用的移植策略和性能优化方案,帮助开发者构建高效、可维护的嵌入式GUI系统。

j2k3l4的博客 922

SPOJ——QTREE2 - Query on a tree II(树链剖分或者倍增法)

Description You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. W

say_c_box的博客 1079

SAP-ABAP:SAP创建CDS视图(SQL语句创建)实例详解

通过Eclipse和ADT创建CDS视图是SAP开发中的一项重要技能。CDS视图不仅简化了数据建模,还提供了高性能和灵活性。掌握CDS视图的开发和使用,可以帮助你更好地支持SAP S/4HANA和Fiori应用的开发。

baidu_35680696的博客 1824

MATLAB - ROS 2 工具箱入门教程

ROS 工具箱提供了一个连接 MATLAB® 和 Simulink® 与机器人操作系统(ROS 和 ROS 2)的接口。利用该工具箱,您可以设计 ROS 节点网络,并将 MATLAB 或 Simulink 生成的 ROS 节点与现有的 ROS 网络相结合。工具箱包括 MATLAB 函数和 Simulink 块,可通过记录、导入和回放 rosbag 文件来可视化和分析 ROS 数据。您还可以连接到实时 ROS 网络,访问 ROS 消息。

机器人控制与仿真应用 3068

SPOJ QTREE2 - Query on a tree II【树链剖分+LCA】

树剖

niiick 370

使用Visual Studio 2022创建CUDA编程项目

本文介绍了在Visual Studio 2022中配置CUDA开发环境的完整流程。首先需要安装VS2022并勾选C++开发组件,然后安装CUDA Toolkit和兼容的NVIDIA驱动。创建项目时选择CUDA模板,VS会自动生成数组相加的示例代码。项目配置包含CUDA头文件依赖和链接器设置(cudart_static.lib)。文章详细说明了从环境搭建到代码编写、调试的完整步骤,帮助开发者快速上手CUDA编程。

新兴IT民工的专栏 1万+

CSU 1414: Query on a Tree

预处理每个结点的子结点的个数sons , 则对x的询问可由sons[x]- sigma( sons[v] ) (v是到x距离为d的点)得到 怎么快速的找到这些v呢? 注意到距离x为d的点肯定在树的同一层.... 可以对树进行dfs时记录每个结点时间戳的同时把每一层的结点保存下来,然后对每一层维护一个前缀和 如果v是x下面子结点那么v的时间戳肯定在x的范围内,这样就可以二分確定出前缀和的范围了.

码代码的猿猿 1613

全功能tgbot/Telegram机器人多功能有后台版源码

trx兑换 闪兑 关键字监控 群管 usdt监控 余额查询 推广分享 等几乎常见的tg机器人功能都有。全功能tgbot/telegram机器人多功能有后台版源码-打包好的可以直接上手。

百创科技 2701

代谢组数据分析(十五):基于python语言构建PLS-DA分类预测模型

本教程阐述典型代谢组学数据分析工作流程,用于二元分类结果的研究。流程主要步骤如下:首先从 Excel 表格导入代谢物与实验数据,接着基于汇总 QC 进行数据清洗。随后借助主成分分析可视化检查数据质量,再开展两类单变量统计。之后运用偏最小二乘判别分析(PLS - DA)实施多变量分析,此过程涵盖模型优化(对比 R2 与 Q2)、置换测试及明确模型预测指标、确定特征重要性、对模型预测数据进行可视化。最终将统计表格导出至 Excel 表格,完整呈现从数据导入到分析结果输出的全过程。

专注生信领域 2875

SPOJ 913 QTREE系列- Query on a tree II (倍增LCA)

题目地址:QTREE2 - Query on a tree II LCA学了离线与在线转RMQ方法后就去做这道题,于是想了好长时间也没想到怎么做。看了题解都是用的倍增LCA。。于是又去学了下倍增法求LCA,这才发现用倍增法做简直是水题。。。因为求路径的第k个点可以转化成求第k个父节点,然而倍增法的原理就是根据的父节点,于是这题就很容易解决了。。 求距离很好求。关键

scf0920 1074

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Query on a tree II 倍增LCA

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will a...

loooooog的博客 135

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

SPOJ QTREE2 Query on a tree II

树上倍增

YHaX的博客 496

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SPOJ Query on a tree II (倍增LCA)

QTREE2 - Query on a tree II #graph-theory #tree You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value

K键盘里的青春K 421

SPOJ913 Query on a tree II

Time Limit:433MS Memory Limit:1572864KB 64bit IO Format:%lld & %llu Description You are given a tree (an undirected acyclic connected graph) withNnodes, a...

dezhen7015的博客 111

SPOJ 913 Query on a tree II

spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B OS Linux Language limit All except: ERL JS-RHINO NODEJS PERL6 VB.NET Source Special thanks to Iva...

weixin_30782331的博客 132

SPOJ 913 Query on a tree II(动态树)

题目链接:http://www.spoj.com/problems/QTREE2/ 题意:给出一棵树,每条边有一个权值。两种操作:(1)询问某两个节点之间的距离;(2)询问x到y的路径上的第K个节点的编号。 思路:节点保存dis值、cnt。对于第二种询问,先access(x),然后y向上,父节点为空时,判断第K个节点在y的左侧右侧还是y本身。 struct node { ...

weixin_34348805的博客 205

【spoj】【QTREE2 - Query on a tree II】

题目大意给出一棵树,每条边有边权,询问两点之间的距离,以及从起点到终点第k个点是那个点。解题思路观察可知,整个询问是静态的,可以使用倍增算法解决。code#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define LF double #define LL long long #define Min(a,

inklutcuah’s blog 411

SPOJ QTREE2 Query on a tree II

LCA #include<cstdio> #include<queue> #include<iostream> #include<cmath> #include<cstring> using namespace std; #define ll long long const int size=10010; int f[size][20],...

weixin_45113721的博客 142

SPOJ Query on a tree II

倍增(lca变式?)

依然一笑作春温。 350

SPOJ QTREE2 Query on a tree II (倍增LCA)

题意: 给定一棵树,q次询问,每次询问路径上第k个点。 思路: 懒得写树剖了。。倍增LCA直接搞。。 #include #include #include #include using namespace std; #define mxn 100020 #define mxe 200200 #define inf 0x3f3f3f3f int fst[mxn], nxt[mxe

yamiedie_ 840
上一篇: Query on a tree I
下一篇: Query on a tree III
diezhan1313
博客等级 码龄11年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值