QTREE2 - Query on a tree II

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
  • KTH a b k : ask for the k-th node on the path from node a to node b

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)


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.


For each "DIST" or "KTH" operation, write one integer representing its result.

Print one blank line after each test.



1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4


hide comments
hkyiitd: 2019-08-03 09:02:01

Don't forget to print one blank line after each test case.

viskey: 2019-05-22 22:16:44

Easy to solve with sqrt decompostion, as there are no updates.

tarungupta: 2019-04-11 10:33:26

Don't forget to clear graph before each test case, if taken globally. Costed me many WA. :/
Binary Lifting implementation works.

vickyand_27: 2019-02-04 19:08:54

Took sosoo long

hacker_sk: 2018-12-29 23:17:51

No need to use long long, int is enough.

alucard_01: 2018-12-24 06:50:25

just use binary lifting

Last edit: 2018-12-24 08:01:13
lamia2658: 2018-11-13 20:16:11

a must do LCA problem after learning

vaishcr7: 2018-08-01 13:18:40

i am getting nzec error in this , i have tried taking input in many different ways. Is anybody else also facing the same problem ?

kspoj: 2018-05-20 10:47:38

Guys, I'm using sqrt decomp for finding LCA and am getting WA (at the very first test)
Please help :(

sggutier: 2017-12-09 00:33:46

Are the test cases correct? For some reason, my solution only works when I send it in C++14 with clang.

I also sent a few solutions to check if the given graph was a tree, and apparently it is not. The submissions for checking if it was not a tree were reading strings. The check submissions were supposed to give WA if it was a tree and TLE if it was not. If the compiler was clang, they gave WA and if it was gcc/g++ it gave TLE.

Maybe that is the reason of why some of you were not getting AC before switching to long long.

btw, I used scanf for reading and printf for printing

Last edit: 2017-12-09 00:36:51

Added by:Thanh-Vy Hua
Time limit:0.433s
Source limit:15000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Special thanks to Ivan Krasilnikov for his alternative solution