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

hide comments
kkkrr: 2019-01-01 14:43:41

Credit goes to Anudeep's blog

hanzhifeng: 2018-12-28 13:54:41

why is the time limit so strict?????

kspoj: 2018-11-30 18:40:55

200+ lines of code...
SIGABRT :(
Can anyone tell me what could be wrong. Toolkit testcases are working fine BTW
:(

kshubham02: 2018-10-21 18:08:20

Are nodes numbered from 1...N as well?

da_201501443: 2018-10-07 19:13:17

Don't make the class, struct in c++ for hld.
It gave me tle.

silxikys: 2018-08-15 22:00:56

if you're getting rte like I was, remember to clear all your maps, adjacency lists, before each test case. Also reset preorder/incrementing variables. pretty annoying stuff, having to deal with multiple test cases

c650: 2018-08-02 22:05:04

I pass in c++ with standard library min/max functions...

mb81: 2018-07-30 08:45:48

BK

spojmaster_9: 2018-07-26 15:14:33

Finally, I did it, after 17 times

pshishod2645: 2018-07-24 16:41:37

Time limit is too strict.
. don't use c++ max function or preprocessor for max. Instead use if or ( ? : ;)
. use char s[], instead of c++ string


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