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 <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int wx=50017;
int dep[wx],dis[wx];
int f[wx][23];
int head[wx];
int num,n,t;
char opt[7];
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
struct e{
int nxt,to,dis;
}edge[wx*2];
void add(int from,int to,int dis){
edge[++num].nxt=head[from];
edge[num].to=to;
edge[num].dis=dis;
head[from]=num;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
f[v][0]=u;dis[v]=dis[u]+edge[i].dis;
dfs(v,u);
}
}
void pre(){
for(int j=1;j<=21;j++)for(int i=1;i<=n;i++)f[i][j]=f[f[i][j-1]][j-1];
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=21;i>=0;i--){
if(dep[f[x][i]]>=dep[y]){
x=f[x][i];
}
}
if(x==y)return x;
for(int i=21;i>=0;i--){
if(f[x][i]!=f[y][i]){
x=f[x][i]; y=f[y][i];
}
}
return f[x][0];
}
int find(int x,int k){
for(int i=21;i>=0;i--){
if(dep[f[x][i]]>=k)x=f[x][i];
}
return x;
}
int main(){
t=read();
while(t--){
n=read();
memset(head,0,sizeof head); num=1;
memset(edge,0,sizeof edge);
for(int i=1;i<n;i++){
int x,y,z;
x=read(); y=read(); z=read();
add(x,y,z); add(y,x,z);
}
dfs(1,0); pre();
while(1){
scanf("%s",opt+1);
if(opt[2]=='O')break;
if(opt[2]=='I'){
int x,y;
x=read(); y=read();
int lca=LCA(x,y);
printf("%d\n",dis[x]+dis[y]-2*dis[lca]);
}
if(opt[1]=='K'){
int a,b,k;
a=read(); b=read(); k=read();
int lca=LCA(a,b);
if(dep[a]-dep[lca]+1>=k)printf("%d\n",find(a,dep[a]-k+1));
else printf("%d\n",find(b,k-dep[a]+2*dep[lca]-1));
}
}
}
}
LCA SP913 QTREE2 - Query on a tree II的更多相关文章
- SP913 QTREE2 - Query on a tree II
思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...
- 【SPOJ QTREE2】QTREE2 - Query on a tree II(LCA)
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
- [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】
题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...
- SPOJ QTREE2 Query on a tree II
传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #defi ...
- spoj 913 Query on a tree II (倍增lca)
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...
- LCA【SP913】Qtree - Query on a tree II
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...
- QTREE2 spoj 913. Query on a tree II 经典的倍增思想
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...
- Query on a tree II 倍增LCA
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
- SPOJ Query on a tree II (树剖||倍增LCA)(占位)
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- Oracle 文件
参数文件 跟踪文件 告警文件 数据文件 临时文件 控制文件 重做日志文件 密码文件 闪回日志 dum文件 数据泵文件 1参数文件 Parameter file:告诉oracle实例在那里可以找到控制文 ...
- DVWA平台v1.9-Brute Force
Low: 随便输一下用户名,密码,test 点击Login 显示用户名或密码错误 在owasp-zap查看数据包 点击,就会转到这 右键,点击Fuzz 点击Remove删除默认的 选定参数变量值,点击 ...
- 【转载】你知道 Linux 内核是如何构建的吗?
内核的根 Makefile 负责构建两个主要的文件:vmlinux (内核镜像可执行文件)和模块文件.内核的 Makefile 从定义如下变量开始: VERSION = PATCHLEVEL = SU ...
- ThinkPad E431按F1后直接进入系统无法进入BIOS
联想的ThinkPad系列笔记本一般是按F1进如BIOS的,但是由于现在联想的笔记本多数都是预装Win 8或者更高版本的系统,所以有时候就没办法直接按F1进去BIOS.其原因是因为Win 8或者更高版 ...
- sizeof总结
1.sizeof常用总结 ①与strlen比较 strlen 计算字符串的字符数,以"\0"为结束判断,但不统计结束符. sizeof 计算数据(数组.变量.类型. ...
- C连接oracle数据库
int db_conn_main() { EXEC SQL BEGEIN DECLARE SECTION; +]; +]; +]; varchar username[]; varchar passwo ...
- Spring IOC容器解析及实现原理
最近一段时间,“容器”两个字一直萦绕在我的耳边,甚至是吃饭.睡觉的时候都在我脑子里蹦来蹦去的.随着这些天一次次的交流.讨论,对于容器的理解也逐渐加深.理论上的东西终归要落实到实践,今天就借助sprin ...
- tomcat8.0的下载安装配置
配置tomcat前要先配置JDK的环境变量 具体方法请点链接JDK环境变量配置 首先要 到官网下载tomcat tomcat官网 进入官网后 如图在左侧选择自己想要下载的版本,这里我以8.0版本为例 ...
- jquery 仿文本编辑器(智能提示输入文字)
1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InputAu ...
- Qemu虚拟机 玩树莓派最新版系统 (截止2017-04-10)
Qemu虚拟机可以玩 树莓派,大家都知道了吧.可是网上的教程好老,都是2012年的.我按照教程下载了最新版版本的树莓派系统怎么也跑不起来. 研究了好久,终于找到一个简单的方法,特意分享出来.转载请注意 ...