SPOJ913 Query on a tree II
| Time Limit: 433MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu | 
Description
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). t test 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
Hint
| Added by: | Thanh-Vy Hua | 
| Date: | 2006-08-27 | 
| Time limit: | 0.433s | 
| Source limit: | 15000B | 
| Memory limit: | 1536MB | 
| Cluster: | Cube (Intel G860) | 
| Languages: | All except: ERL JS NODEJS PERL 6 VB.net | 
| Resource: | Special thanks to Ivan Krasilnikov for his alternative solution | 
有两种操作,一是求两点间距离,二是求一点到另一点路径上的第k个点。
LCA妥妥的。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,dis;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=d;hd[u]=mct;return;
}
int T,n;
int fa[mxn][];
int dep[mxn];
int dis[mxn];
void init(){memset(hd,,sizeof hd);memset(fa,,sizeof fa);mct=;}
void DFS(int u,int f){
dep[u]=dep[f]+;
for(int i=;i<;i++)fa[u][i]=fa[fa[u][i-]][i-];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
fa[v][]=u;
dis[v]=dis[u]+e[i].dis;
DFS(v,u);
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=;i>=;i--)
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return y;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][];
}
inline int dist(int x,int y){//求距离
int tmp=LCA(x,y);
return dis[x]+dis[y]-dis[tmp]*;
}
inline int find(int x,int k){//上溯
for(int i=;i>=;i--){
if(k&(<<i))x=fa[x][i];
}
return x;
}
inline int solve(int x,int y,int k){//查询从x到y路径上第k个结点
int tmp=LCA(x,y);
int mid=dep[x]-dep[tmp]+;
if(k==mid)return tmp;
if(k>mid){
int dd=dep[y]-dep[tmp]+;
mid=k-mid+;
k=dd-mid;
return find(y,k);
}
else
return find(x,k-);
}
int main(){
T=read();
int i,j,x,y,d;
while(T--){
init();
n=read();
for(i=;i<n;i++){
x=read();y=read();d=read();
add_edge(x,y,d);
add_edge(y,x,d);
}
int rt=n/+;
dis[rt]=;
DFS(rt,);
char op[];
while(scanf("%s",op) && (op[]!='D' || op[]!='O')){
if(op[]=='K'){
x=read();y=read();d=read();
printf("%d\n",solve(x,y,d));
}
if(op[]=='D'){
x=read();y=read();
printf("%d\n",dist(x,y));
}
}
}
return ;
}
SPOJ913 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 ...
 - [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】
		
题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...
 - 【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, ...
 - Query on a tree II  倍增LCA
		
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
 - LCA【SP913】Qtree - Query on a tree II
		
Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...
 - SPOJ Query on a tree II (树剖||倍增LCA)(占位)
		
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
 - 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 ...
 - QTREE2 spoj 913. Query on a tree II   经典的倍增思想
		
QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...
 
随机推荐
- 获取iTextSharp 的image 报错
			
获取itextsharp类库的image对象的时候报错 outofmemory .经过艰苦的测试发现jpeg类型是可行的的 iTextSharp.text.Image je = iTextShar ...
 - [TED] New video technology that reveals an objects hidden properties
			
通过视频中,即使1微米的震动,都能够还原成声音. 程序算法结合基础学科,能够发挥出接近无限的力量, 深入挖掘物理特性,形成你想都想不到的效果. 很多技术你都不知道,怎么和国家对抗?所以还是要遵纪守法 ...
 - java之yield(),sleep(),wait()区别详解
			
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
 - 流形学习之等距特征映射(Isomap)
			
感觉是有很久没有回到博客园,发现自己辛苦写的博客都被别人不加转载的复制粘贴过去真的心塞,不过乐观如我,说明做了一点点东西,不至于太蠢,能帮人最好.回校做毕设,专心研究多流形学习方法,生出了考研的决心. ...
 - Qt中forward declaration of struct Ui::xxx的解决
			
每当你新键一个 QT设计界面, QT会自动生成yyy.ui文件,如 <?xml version="1.0" encoding="UTF-8"?> & ...
 - unity3d CarWaypoints插件
			
编写初衷: 1.网上没有现成的好用的waypoints插件 2.自己在做一个赛车游戏,如果没有这款插件的话在制作游戏的过程中会被累成狗 3.从来没有接触过插件方面的东西,所以想自己尝试一下 插件用途: ...
 - Angular指令1
			
Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...
 - 东大OJ-5到100000000之间的回文质数
			
1217: VIJOS-P1042 时间限制: 0 Sec 内存限制: 128 MB 提交: 78 解决: 29 [提交][状态][讨论版] 题目描述 有一天,雄霸传授本人风神腿法 ...
 - Yii2初级入门教程
			
下载安装 Yii挺火的,也是MVC的Web框架.国内占有率,相当不错.值得一学. 网络上提供了两个版本模板的下载, advanced, 和 basic, 使用起来一致, 提供的模块支持不同. Adva ...
 - jQuery基础--样式篇(3)
			
1.jQuiery对象与DOM对象 对于刚刚接触jQuery的初学者,我们要清楚认识一点:jQuery对象与DOM对象是不一样的.可能一时半会分不清楚哪些是jQuery对象,哪些是DOM对象,下面重点 ...