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
- QUERY a b : ask for the maximum edge cost on the path from node a to node b
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.
For each "QUERY" operation, write one integer representing its result.
Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3
Don't use c++ MIN and MAX functions from algorithm, they are very slow, us IF instead!
Very Nice Problem Learnt New Concept C++ working LCA with DP tooLast edit: 2018-02-23 18:52:18
Is c++ solution is submitting on this ??
Credit goes to Anudeep such a nice blog HLD+segment tree.
Did anyone solve this in Python ? Get TLE with HLD…
Java got TLE. Same code in C++ passed. :(
what the fuck...submitted same code 3 times...1 time it got accepted...2 times it got tle...what the fuck spoj..
Guys try to avoid LCA using dp, try doing it with HLD itself, took me 9 submissions to AC
Harish Chandra Thuwal:
Hi can anyone explain how the LCA is implemented in anudeep's (HLD code)blog ?
HLD + segment tree . do read HLD from anudeep's blog.
|Added by:||Thanh-Vy Hua|
|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|