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, ...
随机推荐
- iOS类目、延展和协议
类目:为已知的类增加新的方法:注意:类目里面只能写方法,不能写声明和属性,所以,类目不能作为接口来用 1.类目无法向已有类中添加实例变量.2.如果类目中的方法和已有类中的方法名称冲突时,类目中的方法优 ...
- 数据库:MySQL索引背后的数据结构及算法原理【转】
原文:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话 ...
- 安卓SQLite数据库操作(上)
安卓系统自带数据库,名为SQLite.这篇文章我们用一个Demo来讲解安卓操作数据库的例子. By the way, 安卓创建的数据库文件存放在/data/data/<包名>/databa ...
- 5-EasyNetQ之Publish(黄亮翻译)
EasyNetQ支持的最简单的消息模式是发布/订阅.这个模式是一个极好的方法用来解耦消息提供者和消费者.消息发布者只要简单的对世界说,"这里有些事发生" 或者 "我现在有 ...
- javascript的概述
JavaScript是怎么诞生的???刚开始的是为了验证表单而开发出来的. 什么是JavaScript???a.面向对象的编程语言b.解释性的编程语言(说白了就是不用编译的一种语言)c.脚本语言(说白 ...
- re.match
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- JAVA语法规则总结
单继承多实现 抽象类 抽象方法 使用关键字:abstract修饰的方法就是抽象方法; 抽象方法的形式:只有方法的声明,没有方法体; 抽象方法一般存在于父类中,相当于强制要求子类必须重写该方法,相当于 ...
- IFC数据模式架构的四个概念层详解说明
IFC模型体系结构由四个层次构成,从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain ...
- 在Mac OS里安装和升级Git
在此记录,给自己看,也给别人参考. 进入终端,查看当前Git版本,输入指令:git --version 输入which git回车,可以查看当前git在什么位置 经查,版本:2.10.0,版本较低,为 ...