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, ...
随机推荐
- java详细剖析
1·类型加载主动初始化和被动初始化两种,通过访问静态变量或者给静态变量赋值都是可以使类初始化,如果有继承关系,所依赖的父类都会被动初始化. 2·如果在类的静态变量中添加final关键字,那这个变量就会 ...
- linux /proc/sys/vm/中各个文件含义
1) /proc/sys/vm/block_dump该文件表示是否打开Block Debug模式,用于记录所有的读写及Dirty Block写回动作. 缺省设置:0,禁用Block Debu ...
- css相关整理-其他
1.设备像素(device pixel): 设备像素是物理概念,指的是设备中使用的物理像素.CSS像素(css pixel): CSS像素是Web编程的概念,指的是CSS样式代码中使用的逻辑像素.通过 ...
- linux系统执行mysql脚本:Can't connect to local MySQL server through socket '/tmp/mysql.sock'
问题原因:系统找不到临时文件夹下的.sock文件了 解决办法:看一下是不是其他目录下有mysl的.sock文件,使用命令指定到该文件 mysql --socket=/home/mysql/mysql- ...
- allegro把formate symbol文件从一个文件拷入另一个文件的方法
allegro画好PCB后经常需要添加一些说明谢谢,比如叠层信息.阻抗表等,但是每次都自己画太麻烦,现在就写下如何重复使用各种格式. 1.打开包含这些信息的板子,FILE-> Export -& ...
- 学习animate.css包含了一组炫酷、有趣、跨浏览器的动画
1.animate.css包含了一组炫酷.有趣.跨浏览器的动画,可以在你的项目中直接使用. 第一步:引入animate.css样式文件或者引入某些平台的CDN文件: <head> < ...
- iPhone手机屏幕尺寸(分辨率)
第一代iPhone2G屏幕为3.5英吋,分辨率为320*480像素,比例为3:2. 第二代iPhone3G屏幕为3.5英吋,分辨率为320*480像素,比例为3:2. 第三代iPhone3GS屏幕为3 ...
- input实现上传
很多时候我们会用到上传,实现一个普通的上传也很简单,不用引用繁琐的插件 一个普通的上传 <form action="=upload" method="post&qu ...
- 安卓系统广播暴露设备信息-Android System Broadcasts Expose Device Information
Android device details are being exposed to running applications via Wi-Fi broadcasts in the mobile ...
- springMVC--annotation
一 解析类 ComponentScanBeanDefinitionParser component-scan标签解析类 component-scan 兼容 annotation-config ,因此前 ...