SPOJ QTREE2 (LCA - 倍增 在线)
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.
We will ask you to perfrom some instructions of the following form:
- DIST a b : ask for the distance between node a and node b
or - KTH a b k : ask for the k-th node on the path from node a to node b
Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2
Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)
Input
The first line of input contains an integer t, the number of test cases (t <= 25). ttest cases follow.
For each test case:
- In the first line there is an integer N (N <= 10000)
- In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
- The next lines contain instructions "DIST a b" or "KTH a b k"
- The end of each test case is signified by the string "DONE".
There is one blank line between successive tests.
Output
For each "DIST" or "KTH" operation, write one integer representing its result.
Print one blank line after each test.
Example
Input:
1 6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE Output:
5
3 思路:
倍增裸题。。。套板子,
求第k个的时候需要处理下,其他没什么。,。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 2e5+;
int dist[M],p[M][],dep[M],head[M];
int cnt1,n; struct node{
int to,next,w;
}e[M]; void add(int u,int v,int w){
e[++cnt1].w=w;e[cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
e[++cnt1].w=w;e[cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
} void dfs(int u){
for(int i = head[u];i != -;i=e[i].next){
int v = e[i].to;
if(v == p[u][]) continue;
dep[v] = dep[u] + ;
dist[v] = dist[u] + e[i].w;
p[v][] = u; //p[i][0]存i的父节点
dfs(v);
}
} void init(){
for(int j = ;(<<j)<=n;j++){
for(int i = ;i <= n;i++){
p[i][j] = p[p[i][j-]][j-];
//cout<<i<<" "<<j<<" "<< p[i][j]<<endl;
}
}
} int lca(int a,int b){
if(dep[a] > dep[b]) swap(a,b);
int h = dep[b] - dep[a]; //h为高度差
for(int i = ;(<<i)<=h;i++){ //(1<<i)&f找到h化为2进制后1的位置,移动到相应的位置
if((<<i)&h) b = p[b][i];
//比如h = 5(101),先移动2^0祖先,然后再移动2^2祖先
}
//cout<<a<<" "<<b<<endl;
if(a!=b){
for(int i = ;i >= ;i --){
if(p[a][i]!=p[b][i]){ //从最大祖先开始,判断a,b祖先,是否相同
a = p[a][i]; b = p[b][i]; //如不相同,a,b,同时向上移动2^j
}
}
a = p[a][]; //这时a的father就是LCA
}
return a;
} int kth(int u,int k){
for(int i = ;i < ;i ++)
if(k >> i&)
u = p[u][i];
return u;
} int main()
{
int t,u,v,w,k;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
cnt1 = ;
//init();
memset(head,-,sizeof(head));
for(int i = ;i < n-;i ++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
dfs();
init();
char s[];
while(scanf("%s",s)!=EOF){
if(s[]=='O') break;
scanf("%d%d",&u,&v);
int num = lca(u,v);
if(s[]=='I'){
printf("%d\n",dist[u]+dist[v]-*dist[num]);
}
if(s[]=='T'){
scanf("%d",&k);
int x = dep[u] - dep[num];
if(x + >= k)
printf("%d\n",kth(u,k-));
else printf("%d\n",kth(v,dep[v]+dep[u]-*dep[num]+-k));
}
}
}
return ;
}
SPOJ QTREE2 (LCA - 倍增 在线)的更多相关文章
- LCA(倍增在线算法) codevs 2370 小机房的树
codevs 2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点, ...
- SPOJ DISQUERY LCA + 倍增
裸题,如此之水- Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- 【LCA倍增】POJ1330-Nearest Common Ancestors
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...
- 【codevs2370】小机房的树 LCA 倍增
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0 ...
- LCA倍增算法
LCA 算法是一个技巧性很强的算法. 十分感谢月老提供的模板. 这里我实现LCA是通过倍增,其实就是二进制优化. 任何一个数都可以有2的阶数实现 例如16可以由1 2 4 8组合得到 5可以由1 2 ...
- 洛谷 3379 最近公共祖先(LCA 倍增)
洛谷 3379 最近公共祖先(LCA 倍增) 题意分析 裸的板子题,但是注意这题n上限50w,我用的边表,所以要开到100w才能过,一开始re了两发,发现这个问题了. 代码总览 #include &l ...
- CodeVs.2370 小机房的树 ( LCA 倍增 最近公共祖先)
CodeVs.2370 小机房的树 ( LCA 倍增 最近公共祖先) 题意分析 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天, ...
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
随机推荐
- lvs+dr模式(关键操作)
1) IP配置(配置回环接口添加虚拟ip,让他代为转发) /sbin/ifconfig lo:0 192.168.1.96 broadcast 192.168.1.96 netmask 255.25 ...
- sql语句之表间字段值复制遇到的一些问题--基于mysql
好久没来园子了,转眼2017已经到3月份了,前段时间一直忙没时间写博客(其实是自己懒),感觉内心好惭愧.昨天临下班前,技术老大突然对我说要改下表结构,问我能不能实现将一个表的字段值复制到另外一个表的某 ...
- 20155330 《网络对抗》 Exp9 web安全基础实践
20155330 <网络对抗> Exp9 web安全基础实践 实验问题回答 SQL注入攻击原理,如何防御 原理:SQL注入攻击值得是通过构建特殊的输入作为参数传入web应用程序,而这些输入 ...
- 汇编-MOV指令
知识点: MOV指令 基址 内联汇编 把OD附加到资源管理器右键菜单 一.MOV指令 aaa=0x889977;//MOV DWORD PTR DS:[0x403018],0x8899 ...
- .NET Core 开发之旅 (1. .NET Core R2安装教程及Hello示例)
前言 前几天.NET Core发布了.NET Core 1.0.1 R2 预览版,之前想着有时间尝试下.NET Core.由于各种原因,就没有初试.刚好,前几天看到.NET Core发布新版本了,决定 ...
- Golang Context 详细介绍
Golang context 本文包含对context实现上的分析和使用方式,分析部分源码讲解比价多,可能会比较枯燥,读者可以直接跳过去阅读使用部分. ps: 作者本着开源分享的精神撰写本篇文章,如果 ...
- Final发布 视频展示
1.视频链接 视频地址:http://v.youku.com/v_show/id_XMzk1OTYyNjE0NA==.html?spm=a2hzp.8244740.0.0 杨老师粉丝群——弹球学成语项 ...
- M1事后分析报告
在得到M1团队成绩之后,每个团队都需要编写一个事后分析报告,对于团队在M1阶段的工作做一个总结. 请在2015年11月24日上课之前根据下述博客中的模板总结前一阶段的工作,发表在团队博客上,并在课上的 ...
- Spring学习总结之高级装配
1. profile profile可以决定bean在什么环境下才被装配(开发环境.测试环境.线上环境等) @Profile(“dev”)可以用在class之前,也可以用在类之前(Spring3.2 ...
- Answer My Questions
回答自己的问题,真棒!断电让自己的工作重来.真棒! 阅读以前自己的博客,发现问题都已经有了答案. (1).想要成为一名专业的软件工程师,首先得是有相关的资格证书,这个可以通过软考获得.然后在职场中锻炼 ...