思路

第一个可以倍增,第二个讨论在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的更多相关文章

  1. 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至点 ...

  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. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

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

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

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

  8. SPOJ913 Query on a tree II

    Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  9. 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. Postgres数据库维护

    1.全自动备份 需要在备份机上也安装postgres(最好同一个版本) 在postgres目录下建立密码保存文件(明码保存,所以保密很重要),如果不建立,则因为每次备份都要输入密码,不能进行自动备份 ...

  2. pandas(二)

    层级索引: index=[('a',2010),('b',2011),('c',2010'),('a',2012),('e',2010),('f',2011)] age=[18,17,18,16,18 ...

  3. C语言之指针若干问题

    1.指针变量的赋值问题. 常常有偷懒的小伙子,这样赋值 int *Pointer =  3:/ 这是给Pointer 所指的变量赋值,刚创建Pointer时,它所指的变量是不固定的,可能是某个重要的系 ...

  4. encode和decode区别

    在python2 中是这种,编解码格式.在python3 中编码是会转换成byte类型即只显示ASCII码里的,编码会将byte转换成字符串类型.因此在py3中不需要使用,如果想要特定编码,在文件开头 ...

  5. Leetcode: Find Permutation(Unsolve lock problem)

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  6. Asp.Net Core WebApi 和Asp.Net WebApi上传文件

    public class UpLoadController : ControllerBase { private readonly IHostingEnvironment _hostingEnviro ...

  7. Django的admin管理系统写入中文出错的解决方法/1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘locate’

    Django的admin管理系统写入中文出错的解决方法 解决错误: 1267  Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and ( ...

  8. HashMap 和 Hashtable 的 6 个区别,一般人不知道最后一条

    1.线程安全 Hashtable 是线程安全的,HashMap 不是线程安全的. 为什么说 HashTable 是线程安全的? 来看下 Hashtable 的源码,Hashtable 所有的元素操作都 ...

  9. MIUI系统如何获取ROOT权限

    MIUI系统有么好方法启用了Root超级权限?各位都清楚,Android手机有Root超级权限,一旦手机启用了root相关权限,就能够实现更多的功能,举例子,各位公司的营销部门的同事,使用大多数营销工 ...

  10. php 防跨站表单提交

    一种最优方式防跨站表单提交,用户限时token 就是生成一个随机且变换频繁加密字符串(可逆和不可逆).放在表单中,等到表单提交后检查. 这个随机字符串如果和当前用户身份相关联的话,那么攻击者伪造请求会 ...