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) ...
随机推荐
- Landen邀请码
Y2PZ6U8 landen 输入邀请码,注册一年会额外赠送一个月,注册两年会额外赠送三个月.
- qemu安装
1 下载qemu wget http://wiki.qemu-project.org/download/qemu-2.4.1.tar.bz2 2 解压qemu tar -jxvf qemu-2.4.1 ...
- 【CobaltStrike】CobaltStrike简介与使用
CobaltStrike简介与使用 0x00 简介 1. 什么是CobaltStrike?可以用来做什么? CobatStrike是一款基于java编写的全平台多方协同后渗透攻击框架.CobaltSt ...
- Trusted Cloud Summit(2018.08.14)
时间:2018.08.14地点:北京国际会议中心
- 20155330 《网络对抗》 Exp8 Web基础
20155330 <网络对抗> Exp8 Web基础 实验问题回答 什么是表单 表单可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一个表单有三个基本组成部分 表单标签 ...
- mfc CCombox系统定义成员函数
通过ID操作对象 CComboBox(组合框)控件 CComboBox类常用成员 CComboBox插入数据 CComboBox删除数据 CComboBox运用示例 一.CComboBox控件常用属性 ...
- python 带参数的多重继承
1. 不带参数的多重继承 # 作者:hhh5460 # 时间:2017.07.18 class A(object): def show_x(self): print('A') class B(obje ...
- Redis简介与Memcached的比较
Redis简介 Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redis纯粹为应用而产生,它是一个高性能的 ...
- 复习下VLAN的知识
转载:来自百度百科 VLAN一般指虚拟局域网 VLAN(Virtual Local Area Network)的中文名为"虚拟局域网". 虚拟局域网(VLAN)是一组逻辑上的设备和 ...
- Intellij Idea 创建JavaWeb项目入门(一)
Idea创建JavaWeb项目步骤:1.打开Intellij Idea IDE,然后点击Create New Project 2.左侧选择Java Enterprise,右侧选择Web Applica ...