SP913 QTREE2 - Query on a tree II
思路
第一个可以倍增,第二个讨论在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(int ui,int vi,int wi){
++cnt;
v[cnt]=vi;
w[cnt]=wi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void dfs(int u,int f,int wx){
dep[u]=dep[f]+1;
jump[u][0]=f;
sum[u][0]=wx;
for(int i=1;i<15;i++)
jump[u][i]=jump[jump[u][i-1]][i-1],sum[u][i]=sum[u][i-1]+sum[jump[u][i-1]][i-1];
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==f)
continue;
dfs(v[i],u,w[i]);
}
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
for(int i=15;i>=0;i--)
if(dep[jump[x][i]]>=dep[y])
x=jump[x][i];
if(x==y)
return x;
for(int i=15;i>=0;i--)
if(jump[x][i]!=jump[y][i])
x=jump[x][i],y=jump[y][i];
return jump[x][0];
}
int query_sum(int x,int y){
int ans=0;
if(dep[x]<dep[y])
swap(x,y);
for(int i=15;i>=0;i--)
if(dep[jump[x][i]]>=dep[y]){
ans+=sum[x][i];
x=jump[x][i];
}
if(x==y)
return ans;
for(int i=15;i>=0;i--)
if(jump[x][i]!=jump[y][i]){
ans+=sum[x][i]+sum[y][i];
x=jump[x][i],y=jump[y][i];
}
return ans+sum[x][0]+sum[y][0];
}
int kth_fa(int x,int k){
for(int i=15;i>=0;i--){
if((k>>i)&1)
x=jump[x][i];
}
return x;
}
void init(void){
memset(jump,0,sizeof(jump));
memset(sum,0,sizeof(sum));
memset(fir,0,sizeof(fir));
memset(nxt,0,sizeof(nxt));
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
cnt=0;
memset(dep,0,sizeof(dep));
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=1;i<n;i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
dfs(1,0,0);
char opt[10];
while(1){
scanf("%s",opt);
if(opt[1]=='O')
break;
if(opt[1]=='I'){
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",query_sum(a,b));
}
else{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int LCA=lca(a,b);
if(dep[a]-dep[LCA]+1>=c){
printf("%d\n",kth_fa(a,c-1));
}
else{
c-=dep[a]-dep[LCA];
int t2=dep[b]-dep[LCA]+1;
printf("%d\n",kth_fa(b,t2-c));
}
}
}
}
return 0;
}
SP913 QTREE2 - Query on a tree II的更多相关文章
- 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至点 ...
- 【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 ...
- QTREE2 spoj 913. Query on a tree II 经典的倍增思想
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...
- LCA【SP913】Qtree - Query on a tree II
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...
- SPOJ913 Query on a tree II
Time Limit: 433MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- Query on a tree II 倍增LCA
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- python NLTK安装
stanford nltk在python中如何安装使用一直都很神秘,看了一些帖子感觉讳莫如深.研究了几天,参考<nlp汉语自然语言处理原理与实践>,发现方法如下: 1.安装JAVA 8+环 ...
- mysql(七)
多表查询: 显示内连接: select 字段列表 from 表名1 inner join 表名1 on 条件 * inner 可忽略 select * from student inner j ...
- 使用纳米 Protocol buffers 作为序列化数据
使用纳米 Protocol buffers 作为序列化数据 Protocol Buffers 是 Google 公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化. 但是它更小, 更快, ...
- OAuth2.0配置
一:授权服务器相关代码 AuthorizationServer.java import org.springframework.beans.factory.annotation.Autowired; ...
- jQuery的属性操作
下面介绍jQuery属性操作: .val() 这是一个读写双用的方法,用来处理input的value,当方法没有参数的时候返回input的value值,当传递了一个参数的时候,方法修改input的va ...
- Oracle 12C执行root.sh CLSRSC-119 CRS-8503 CLSRSC-366
1.环境 OS:SUSE Linux Enterprise 12 SP3 X86_64 (Kernel: 4.4.162-94.69.2) DB:12.2.0.1 两节点RAC 参考文档:在 SLES ...
- [INS-06006] Passwordless SSH connectivity not set up between the following node(s)
解决方法1 参考:11.2.0.4 runInstaller: [INS-06006] Passwordless SSH connectivity not set up between the fol ...
- Java中的过滤器,拦截器,监听器---------简单易懂的介绍
过滤器: 过滤器其主要特点在于:取你需要的东西,忽视那些不需要的东西!在程序中,你希望选择中篇文章中的所有数字,你就可以针对性的挑选数字! 拦截器: 拦截器其主要特点在于:针对你不要的东西进行拦截,比 ...
- IDEA搭建scala开发环境开发spark应用程序
通过IDEA搭建scala开发环境开发spark应用程序 一.idea社区版安装scala插件 因为idea默认不支持scala开发环境,所以当需要使用idea搭建scala开发环境时,首先需要安 ...
- spark-shell的Scala的一些方法详解
Tom,DataBase,80 Tom,Algorithm,50 Tom,DataStructure,60 Jim,DataBase,90 Jim,Algorithm,60 Jim,DataStruc ...