LCA SP913 QTREE2 - Query on a tree II】的更多相关文章

SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾. 裸的LCA. 在处理第二个操作时,我直接向上数跳了多少个. 顾z大佬说不能这么做,要求出跳到那个点的深度再去跳. 真的是这样,不过懒得想了,应该是+1-1的误差. balabala... code: #include <iost…
思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int jump[10010][16],sum[10010][16],fir[10010],nxt[10010*2],v[10010*2],w[10010*2],cnt,dep[10010],n; void addedge…
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: D…
题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各个节点之间的距离,然后倍增LCA求解就可以了. 那么第二问我WA了6发,原来是眼瞎和手残打错了两个字符错掉了. 我们将问题分成3个部分: LCA是第k个 第k个在u到LCA的路径上 第k个在LCA到v的路径上. 首先如果LCA是第k个,那么直接输出. 如果是第二种情况,那么从u开始做倍增,每一次k-…
传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) <<],*p1=buf,*p2=buf; inline int read(){ #define num ch-'0'…
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 th…
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾. Input 第一组数据包含一个整数\(T\),代表有\(T\)组测试数据.\(1\leq T \leq 25\) 对每一组测试数据: 第一行一个整数\(N(n \leq 10000)\) 接下来有\(N-1\)行,每一行描述树上的一条边\(a,b,c( c\leq 100…
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每个询问(x,y),想求得lca(x,y),直接用dis[x]+dis[y]-2*dis[ lca(x,y) ]即可. 第二问的话,可以用倍增的方式求.我们通过求得节点x,y,lca(x,y)的深度,判断第k个节点落在哪个链上,该链是指是从x到根或者从y到根.最后倍增可以轻松求出一个链上第k个父亲是谁…
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: D…
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: D…