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的更多相关文章

  1. SP913 QTREE2 - Query on a tree II

    思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...

  2. 【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, ...

  3. [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】

    题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...

  4. SPOJ QTREE2 Query on a tree II

    传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #defi ...

  5. 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 ...

  6. LCA【SP913】Qtree - Query on a tree II

    Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...

  7. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

    QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...

  8. Query on a tree II 倍增LCA

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  9. 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, ...

随机推荐

  1. iOS中的数据存储

    SQLite3 SQLite3是一款开源的嵌入式关系型数据库,可移植性好,易使用,内存开销小. SQLite3是无类型的,意味着你可以保存任何类型的数据到任意表的任意字段中. SQLite3常用的4种 ...

  2. 图解缓存淘汰算法二之LFU

    1.概念分析 LFU(Least Frequently Used)即最近最不常用.从名字上来分析,这是一个基于访问频率的算法.与LRU不同,LRU是基于时间的,会将时间上最不常访问的数据淘汰;LFU为 ...

  3. navicat for mysql ,mysql版本是8.0的版本,连接数据库报错1251,解决办法。

    我的mysql版本是8.0的版本,因为毕竟新的mysql采用新的保密方式,所以就的似乎不能用,改密码方式: 用管理员身份打开cmd mysql -uroot -p(输入密码)            进 ...

  4. 2016.8.11 DataTable合并及排除重复方法

    合并: DataTable pros=xxx; DataTable pstar=yyy; //将两张DataTable合成一张 foreach (DataRow dr in pstar.Rows) { ...

  5. Java堆初始大小的建议值

    摘自:<Java Performance>第三章 Initial Heap Space Size Configuration This section describes how to u ...

  6. 10-31SQLserver基础--聚合函数、分组

    在查询语句时,也存在一些方法和属性,而这些方法在查询时统称为函数,便利查询时使用 聚合函数(都是针对字段操作) 聚合是缩减一系列输入值的表达式,例如缩减为单个值. Select*from biao 1 ...

  7. DAY17-Django之model查询

    查询表记录 看专业的官网文档,做专业的程序员! 查询相关API <1> all(): 查询所有结果——QuerySet <2> filter(**kwargs): 它包含了与所 ...

  8. IE9以及IE9以下,无法执行innerHTML这一操作的解决方法

    例如:在select下无法用innerHTML添加<option> 解决代码: var s=document.createElement("option"); s.te ...

  9. 【271】IDL-ENVI二次开发

    参考:String Processing Routines —— 字符串处理函数 01   STRING 返回字符串. 02   STRCMP 比较字符串,一样返回1,不一样返回0,默认大小写敏感. ...

  10. 批处理基本知识以及进阶 V2.0

    批处理基本知识以及进阶 将以要执行的程序指令 , 像在 dos 模式下一下写入记事本 , 保存成 bat 文件 , 就可以执行了 一 . 简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求 ...