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

uttom: 2015-01-27 06:21:18

I got WA
here is my code
http://ideone.com/A2JTRr

Last edit: 2015-01-27 06:29:36
changjianbin: 2015-01-25 02:51:45

233

Prime: 2015-01-01 16:15:15

This problem is so good!My program is accpeted!But I want to know how to solve it by link cut tree.Who can help me?

hippie: 2014-12-28 16:57:57

Don't use string and cin for the words in input --> will cause TLE. Use char array and scanf instead. Wasted 4 hours on such a stupid issue.

Last edit: 2014-12-28 16:58:25
‡■■Lãng du■■‡: 2014-10-30 17:04:44

My solution, complexity O(logn ^2) for each "query" and O(logn) for each "Change"??? Is the time limit so low ??

Last edit: 2014-10-30 22:54:43
r64: 2014-08-06 04:11:59

How many queries are there at most?

zicowa: 2014-07-08 16:37:09

:D


Added by:Thanh-Vy Hua
Date:2005-06-08
Time limit:0.851s
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 qobi SCM guile ST TEXT WHITESPACE