Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Submit Status

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 ab 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
/**
题意:给一个树,求u->v的距离
求u->v的第k个点
做法:专题是树链划分 但是想想LCA可以求距离 第k个点 要么是u->v的第k个点 要么是第k`个点
**/
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
const int maxn = ;
const int DEG = ;
int main();
struct Edge
{
int to;
int nxt;
int val;
} edge[maxn * ];
int head[maxn], tot;
int mmap[maxn];
void addedge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].val = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void init()
{
tot = ;
memset(head, -, sizeof(head));
}
int fa[maxn][DEG];
int deg[maxn];
void bfs(int root)
{
queue<int>que;
deg[root] = ;
mmap[root] = ;
fa[root][] = root;
que.push(root);
while(!que.empty())
{
int tmp = que.front();
que.pop();
for(int i = ; i < DEG; i++) {
fa[tmp][i] = fa[fa[tmp][i - ]][i - ];
}
for(int i = head[tmp]; i != -; i = edge[i].nxt)
{
int v = edge[i].to;
if(v == fa[tmp][]) {
continue;
}
deg[v] = deg[tmp] + ;
mmap[v] = mmap[tmp] + edge[i].val;
fa[v][] = tmp;
que.push(v);
}
}
}
int LCA(int u, int v)
{
if(deg[u] > deg[v]) {
swap(u, v);
}
int hu = deg[u];
int hv = deg[v];
int tu = u;
int tv = v;
for(int det = hv - hu, i = ; det; det >>= , i++)
if(det & ) {
tv = fa[tv][i];
}
if(tu == tv) {
return tu;
}
for(int i = DEG - ; i >= ; i--)
{
if(fa[tu][i] == fa[tv][i]) {
continue;
}
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][];
}
bool flag[maxn];
int query(int u, int v, int k)
{
int root = LCA(u, v);
int ans ;
int i, j;
// cout << deg[u] << " " << deg[root] << endl;
if(deg[u] - deg[root] + >= k)
{
ans = deg[u] - k + ;
for(i = ; ( << i) <= deg[u]; i++);
i--;
for(j = i; j >= ; j--)
{
if(deg[u] - ( << j) >= ans)
{
u = fa[u][j];
}
}
return u;
}
else
{
ans = deg[root] + k - (deg[u] - deg[root] + );
cout << ans << endl;
for(i = ; ( << i) <= deg[v]; i++);
i--;
for(j = i; j >= ; j--)
{
if(deg[v] - ( << j) >= ans)
{
v = fa[v][j];
}
}
return v;
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
int u, v, w;
memset(flag, false, sizeof(flag));
init();
for(int i = ; i < n - ; i++)
{
scanf("%d %d %d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
flag[v] = true;
}
int root;
for(int i = ; i <= n; i++)
{
if(!flag[i])
{
root = i;
break;
}
}
bfs(root);
char ch[];
int uu, vv, ww;
while()
{
scanf("%s", ch);
if(strcmp(ch, "DONE") == ) {
break;
}
else if(strcmp(ch, "DIST") == )
{
scanf("%d %d", &uu, &vv);
// cout << deg[uu] << " " << deg[vv] << endl;
// cout << LCA(uu, vv) << ".......\n";
printf("%d\n", mmap[vv] + mmap[uu] - * mmap[LCA(uu, vv)]);
}
else
{
scanf("%d %d %d", &uu, &vv, &ww);
printf("%d\n", query(uu, vv, ww));
}
}
}
return ;
}

SPOJ-913的更多相关文章

  1. 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 ...

  2. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

    QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...

  3. 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 ...

  4. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  5. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  8. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  9. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  10. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

随机推荐

  1. php中数据类型的强制转换

    1.在PHP开发种在很多的地方要涉及到数据类型的转换,尤其是涉及到金额的数据类型,一定要转换成float类型,否则在入库的时候可能会因为数据类型的不同覆盖掉之前的金额.(字符串和float类型相加) ...

  2. Linux 简单socket实现UDP通信

    服务器端 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sy ...

  3. PAT 甲级 1007 Maximum Subsequence Sum

    https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K  ...

  4. sudo是干哈子的

    我sudo loop发现啊大家就都是root了,那么这和我直接用root起有啥区别呢? root      3826  0.0  0.1  56596  3984 pts/2    S+   12:5 ...

  5. 浅拷贝&深拷贝&Copy On Write(Sring类)

    String类的三种实现 浅拷贝 class String { public: String(const char* pdata)//构造函数 :_pdata(]) { strcpy(_pdata, ...

  6. arc073 F many moves(dp + 线段树)

    设dp[i][y]表示一个点在x[i],另一个点在y时最小要走的步数 那么有以下转移 对于y != x[i-1]的状态,可以证明,他们直接加|x[i] - x[i-1]|即可(如果有其他方案,不符合对 ...

  7. 【考试记录】4.8 Path (网络流 —— 劲题)

    手抄代码 + 学习指针 + 冥思苦想一晚上终于——在一瞬间开窍了.果然题目都是这样:突破了一个点,一切都是柳暗花明. 题面描述: 样例: 这道题目,首先注意到给定的边的性质:这些边在平面上构成了一棵树 ...

  8. [CF1076E]Vasya and a Tree

    题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$.最后输出每个节点的值 题解:可以把操作离线,每次开始遍历到一个 ...

  9. [Leetcode] Interger to roman 整数转成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. BZOJ 4710 [Jsoi2011]分特产 解题报告

    4710 [Jsoi2011]分特产 题意 给定\(n\)个集合,每个集合有相同的\(a_i\)个元素,不同的集合的元素不同.将所有的元素分给\(m\)个不同位置,要求每个位置至少有一个元素,求分配方 ...