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个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...
随机推荐
- Handler 消息传递机制
1,Handler 的概念Handler 是用来干什么的?1)执行计划任务,可以在预定的时间执行某些任务,可以模拟定时器 2)线程间通信.在Android的应用启动时,会创建一个主线程,主线程会创建一 ...
- 文本比较算法Ⅱ——Needleman/Wunsch算法
在"文本比较算法Ⅰ--LD算法"中介绍了基于编辑距离的文本比较算法--LD算法. 本文介绍基于最长公共子串的文本比较算法--Needleman/Wunsch算法. 还是以实例说明: ...
- [转]关于Python中的yield
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...
- Theano1.1-安装
之前一直想弄theano,可是python不是很懂,在学习了一段时间之后开始安装theano.当然官网上的安装资料是全,可是也太繁琐了.这里介绍的是最简单,最方面的安装theano的方法.官网首页:h ...
- 如何在 apache 中设置缓存有效时间
今天学习了下如何在 apache 中设置缓存时间,记之以备忘. 在 http 报文头中,与缓存时间有关的两个字段是 Expires 以及 Cache-Control 中的 max-age,Expire ...
- 判断Laravel Eloquent获取数据结果集是否为空
在使用Laravel Eloquent模型时,我们可能要判断取出的结果集是否为空,但我们发现直接使用is_null或empty是无法判段它结果集是否为空的. var_dump之后我们很容易发现,即使取 ...
- 使用HttpWebRequest和HtmlAgilityPack抓取网页(拒绝乱码,拒绝正则表达式)
废话不多说, 直接说需求. 公司的网站需要抓取其他网站的文章,但任务没到我这,同事搞了一下午没搞出来.由于刚刚到公司, 想证明下自己,就把活揽过来了.因为以前做过,觉得应该很简单,但当我开始做的时候, ...
- [leetcode]算法题目 - Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- VR的UI、UX设计原则
国外其实有不少关于VR用户体验的研究 总结一下我所了解的: Cardboard Design Lab 1. 使用十字线(比较适用于移动VR.一体机) 2.有深度的UI与眼睛疲劳: 离眼睛近的UI,物体 ...
- C# 调用一个按钮的Click事件(利用反射)
最基本的调用方法 (1)button1.PerformClick();(2)button1_Click(null,null);(3)button_Click(null,new EventArgs()) ...