SPOJ QTREE2 Query on a tree II
倍增水题……
本来还想用LCT做的……然后发现根本不需要
//minamoto
#include<bits/stdc++.h>
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
inline bool isop(char ch){
return ch=='I'||ch=='H'||ch=='O';
}
inline char readop(){
char ch;
while(!isop(ch=getc()));
return ch;
}
const int N=;
int head[N],Next[N<<],edge[N<<],ver[N<<],fa[N][],d[N],dist[N];
int n,m,tot;
inline void add(int u,int v,int e){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=e;
}
void dfs(int u,int f){
d[u]=d[f]+,fa[u][]=f;
for(int i=;(<<i)<=d[u];++i) fa[u][i]=fa[fa[u][i-]][i-];
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(v==f) continue;
dist[v]=dist[u]+edge[i],dfs(v,u);
}
}
int LCA(int x,int y){
if(d[x]<d[y]) swap(x,y);
for(int i=;i>=;--i)
if(d[fa[x][i]]>=d[y]) x=fa[x][i];
if(x==y) return x;
for(int i=;i>=;--i)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][];
}
int querylen(int x,int y,int k){
int lca=LCA(x,y);
if(d[x]-d[lca]+>=k){
int ans=d[x]-k+;
for(int i=;i>=;--i)
if((<<i)<=d[x]-ans) x=fa[x][i];
return x;
}
else{
int ans=d[lca]*+k-d[x]-;
for(int i=;i>=;--i){
if(d[y]-(<<i)>=ans) y=fa[y][i];
}
return y;
}
}
int main(){
//freopen("testdata.in","r",stdin);
int q=read();
while(q--){
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
d[]=dist[]=,tot=;
int n=read();
for(int i=;i<n;++i){
int u=read(),v=read(),e=read();
add(u,v,e);
}
dfs(,);
bool flag=true;
while(flag){
char op=readop();
switch(op){
case 'O':flag=false;break;
case 'I':{
int u=read(),v=read();
int lca=LCA(u,v);
printf("%d\n",dist[u]+dist[v]-*dist[lca]);
break;
}
case 'H':{
int u=read(),v=read(),k=read();
printf("%d\n",querylen(u,v,k));
break;
}
}
}
puts("");
}
return ;
}
SPOJ 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 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 ...
- 【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, ...
- QTREE2 spoj 913. Query on a tree II 经典的倍增思想
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...
- SPOJ 913 Query on a tree II
spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B ...
- SP913 QTREE2 - Query on a tree II
思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...
- [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】
题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 【BZOJ2589】 Spoj 10707 Count on a tree II
BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
随机推荐
- ShaderLab内置变量
[ShaderLab内置变量]
- vue的样式绑定
vue在样式绑定,看这官方的文档,怎么试都不行后来看了一篇文章 <div :class="[rankClass]"></div> <script> ...
- zoj1001-A + B Problem
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1 A + B Problem Time Limit: 2 Seconds ...
- ubuntu 源码安装 lnmp 环境
准备篇 下载软件包 1.下载nginx http://nginx.org/download/nginx-1.2.0.tar.gz 2.下载pcre (支持nginx伪静态) ftp://ftp.cs ...
- SpringCloud04 服务配置中心、消息总线、远程配置动态刷新
1 环境说明 JDK:1.8 MAVENT:3.5 SpringBoot:2.0.5.RELEASE SpringCloud:Finchley.SR1 2 创建服务注册中心(Eureka服务端) 说明 ...
- 15- 1 << k 时的益出
扩展GCD-时间复杂性 题目: 计算循环语句的执行频次 for (i = A; i != B; i += C) x += 1;其中A, B, C, i都是k位无符号整数. 输入: A B C k, 其 ...
- 日志管理,springboot
1.市面上的日志框架:JUL.JCL.Jboss-logging.logback.log4j.log4j2.slf4j.... 2. 日志门面: SLF4J:日志实现:Logback:SpringBo ...
- cakephp跳转到指定的错误页面
第一步:修改core.php 第二步:创建AppExceptionRender.php文件 参考:https://blog.jordanhopfner.com/2012/09/11/custom-40 ...
- ubuntu虚拟机安装简单pxe服务器
安装环境: vmware2台虚拟机,一台用来做pxe服务器(安装tftp.dhcp.apache2等服务),另一台用来做安装测试. 虚拟机的网络配置:dhcp的虚拟网络为192.168.10.100/ ...
- How attach Java source(为eclipseIDE附加资源)
In Eclipse, when you press Ctrl button and click on any Class names, the IDE will take you to the s ...