QTREE - Query on a tree

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). 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 <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3

Added by:Thanh-Vy Hua
Date:2005-06-08
Time limit:1s
Source limit:15000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ADA95 ASM32 BASH BF C CSHARP CPP CLPS LISP sbcl LISP clisp D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM guile SCM qobi ST TEXT WHITESPACE

hide comments
2021-08-23 17:59:05
AC, but HLD is just pain
2021-02-16 17:20:28
best place to study heavy light decompostion
https://discuss.codechef.com/t/tutorial-heavy-light-decomposition/69423
https://www.youtube.com/watch?v=LK-0QNShaPM
2020-08-30 16:16:41
Not kidding. What's HLD? Please tell me...

Last edit: 2020-08-30 16:17:01
2020-07-09 23:14:38
Nice problem to learn HLD :)
2020-05-01 10:04:49
If you are getting RE SIGSEGV, use void return type with functions not returning anything.
2020-03-22 19:46:14
There's no need to find LCA separately if you are using HLD.
2020-02-18 05:18:44
I add ios_base::sync_with_stdio(false);cin.tie(NULL); and TLE becomes AC
2020-01-06 11:40:24
getting tle on cpp is that ok

upd 11.27.2020 ok i solved it xd

Last edit: 2020-11-27 18:56:06
2019-12-05 10:34:33
See this as well.
https://acm.timus.ru/problem.aspx?space=1&num=1553
2019-11-28 09:41:34
if you get TLE, just optimize function max, dfs & hld decomposition like this guy : https://ideone.com/Ykg63S
it's kind of surprised but this made my code run faster & I got AC after 1000 times tried.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.