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
amira3: 2024-08-10 11:54:43

Use query decomposition and decompose each query into sqrt(number of queries) and for each block of queries, process DFS and sparse table, and for every change in each query, after changing it to answer other queries you can just check if the edge is in the path from a to b or no (you can check this using LCA) and etc, overall time complexity : O(n * sqrt * log)

Last edit: 2024-08-10 12:31:03
luozhihe: 2024-01-09 16:05:54

TLE c++ please help me
<snip>
[Simes]: this is not the place for debugging code, use the forum.

Last edit: 2024-01-09 19:31:19
tobiasveiga: 2023-12-01 13:40:59

TIP: Assume nodes are numbered starting from 0 (unlike the edges...)

saiprasad_27: 2022-06-21 00:32:34

@t_l_enough same it is running fine on my computer but don't know why I'm getting SIGSEGV :(...
Got ac after updating curr_idx=0;
in every tetcase :)

Last edit: 2022-06-24 07:56:06
vladislav11: 2022-06-08 18:44:41

https://cp-algorithms.com/graph/hld.html#implementation

Last edit: 2022-06-08 18:45:01
badmachine77: 2022-05-08 14:20:42

does anyone with AC want to get in touch and send me a solution because I am really struggling on this one ;(

kanisht09: 2022-01-11 20:40:23

thanks @princemishra

t_l_enough: 2021-11-27 09:03:06

Why does my code(c++) runtime error (SIGSEGV)?

Last edit: 2021-11-27 09:05:23
whhsteven: 2021-10-31 08:24:39

Well, thank you.

Last edit: 2021-10-31 08:25:03
diwakar0: 2021-08-25 07:58:20

TLE in JAVA same code in CPP is ACCEPTED


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