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
naimsantos: 2019-11-24 18:58:55

Dont know why, but when i used HLD with implicit LCA got TLE, but when I used the LCA got AC.

peregrine: 2019-09-12 08:47:13

Ignore the comments. Just don't use cin/cout. Nice problem to start HLD.

Last edit: 2019-09-13 09:12:40
umang696: 2019-09-09 13:19:14

I am getting RTE in java even with inputs.
Is there any error in the test set??

quachtridat: 2019-08-29 22:11:49

AC'd with a lot of debugging. SPOJ toolkit recommended.

@ksubham02 I used pairs and dot operators in my code and still got AC verdict. I used std::min and std::max functions, use ceiling value of log base 2 of 2E4 and just some bitwise ops for dividing/multiplying by 2. My code ran without problems (at least after debugging).

Last edit: 2019-08-30 00:29:13
omar_alhelo: 2019-08-10 19:37:58

If you're still getting TLE or RTE with c++, make sure to return 0 in main and use void return type with corresponding functions.

kshubham02: 2019-07-12 02:58:50

This is crazy. In the end I made optimizations like
--- change vector of pairs into two vectors of ints to avoid using dot operator
--- change max/min function calls to if else statements
--- change arithmetic operators to bitwise operators
--- change approx value of log(10^4) from 20 to 14
I even wrote a program to generate test cases of maximum size which is currently building 20 random trees of size 10000 and giving 10000 queries. All of it is passing in 0.5sec in my machine but giving TLE here.

I copied and pasted Anudeep's solution and AC.
Crazy. Absolutely crazy.

coldbeer: 2019-07-02 20:01:34

yes there is no issue with struct my code is also working...i have implemented lca,hld and segtree using struct

armoking: 2019-06-13 15:20:04

Well, there is all OK with c++ struct - I have 0.33 sec AC. There are no negative weights. For query like (a, a), I have answer 0. So - it is good standard task. Author, thank you!

Last edit: 2019-06-13 15:22:23
Md. Abdulla Al Mamun (Nayon): 2019-06-04 10:09:19

c++11 features support, max function used AC in 0.29s

malviyanshiv: 2019-02-08 17:33:51

I was forced to use scanf and printf...


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