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
linkret: 2016-03-22 16:17:44

HLD is Heavy - Light Decomposition. Alot of code.

austine: 2016-03-03 10:34:01

Classical problem

Obliterator: 2015-11-05 06:45:11

Huge number of accepted solutions is because of Anudeep's blog explaining HLD very clearly http://blog.anudeep2011.com/heavy-light-decomposition/

Last edit: 2015-11-06 12:07:05
kingston: 2015-10-08 18:38:59

longest code ever written by me

(Tjandra Satria Gunawan)(曾毅昆): 2015-07-27 23:36:30

woah, this hard problem is solved by 1775++ users (o_O)
Need 10+ hours for me to solve this problem using data structure much more complicated than SID, my code even very long* (~200 lines; ~3.5 KB; writen in C)..
*compared to my average submission size

I wonder if there are easier way to solve this problem, considering huge number of accepted users, maybe my algo is overkill (because my partially optimized code is #4 fastest) or maybe semi brute-force is enough pass(?), just curious :p

yyy_ugly: 2015-04-15 08:50:44

yyy handsome ahead..............

lcr88888: 2015-04-14 13:55:35

YYY is handsome.

Last edit: 2015-04-14 14:08:43
lcr88888: 2015-04-14 13:55:26

呔!

Last edit: 2015-04-14 14:11:26
chenyushuo: 2015-04-01 03:08:18

I have got Accepted!

Last edit: 2015-04-01 03:09:30
krish: 2015-02-17 22:07:37

very good problem.
Segment tree+HLD..


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