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 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...
随机推荐
- iPhone与Android手机 各个型号的UserAgent
摘要:userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值.一般来讲,它是在navigator.appCodeName 的值之后加上斜线和navigator.a ...
- 201671010127 2016-2017-11 Java图形用户界面设计技术
一.事件处理器 1.什么是事件处理 一个事件要求特定的动作被执行,它被作为消息由外界或系统自身发送给GUI系统.这些事件包括来自计算机设备如鼠标键盘和网络端口的I/O中断,以及GUI系统的逻辑事件触发 ...
- DevCloud for CloudStack Development
Apache CloudStack development is not an easy task, for the simplest of deployments one requires a se ...
- 19-字符切割函数c++模板
https://www.cnblogs.com/stonebloom-yu/p/6542756.html #include <cstring> #include <cstdio> ...
- tensorflow学习笔记----TensorBoard讲解
TensorBoard简介 TensorBoard是TensorFlow自带的一个强大的可视化工具,也是一个Web应用程序套件.TensorBoard目前支持7种可视化,Scalars,Images, ...
- $.ajax()函数
一般在前端html和服务器交互,又要异步提交表单时,我们通常会用到$.ajax(){}函数,这是封装到ajax里的一个函数,相比于XMLHTTPRequest做页面局部刷新更方便,但最终还是使用的XM ...
- 【原创】cython and python for kenlm
未经允许不可转载 Kenlm相关知识 Kenlm下载地址 kenlm中文版本训练语言模型 如何使用kenlm训练出来的模型C++版本 关于Kenlm模块的使用及C++源码说明 加载Kenlm模块命令 ...
- elasticsearch-jdbc 使用
elasticsearch-jdbc是一个将关系型数据库(RDBMS)数据导入到ElasticSearch库中的一个工具包,支持mysql.oracle.postgrey.csv等存储列式数据的容器. ...
- mysql元数据查询
截图来自<深入浅出MySQL++数据库开发.优化与管理维护+第2版+唐汉明>一书
- mysql问题,出现 Cant connect to mysql server on 'localhost'
莫名其妙的一个问题,这个问题出现在今天,然后查找下,发现需要重启服务器,但是重启也一样,于是关机重启,还是这个现象 ,然后看到 错误提示, 提示my.ini的第21行,产生错误,于是按照路径找到配置文 ...