Batman and Tree

All submissions for this problem are available.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Nazik gave Batman a tree with $N$ vertices (numbered $1$ through $N$). There are some robbers in the tree; of course, Batman wants to catch the robbers. Initially, Batman is in a vertex $s$ and his power is equal to $p$. During each of the next $M$ seconds, the following process happens:  A robber with power $r$ runs from a vertex $x$ straight to vertex $y$.  Batman either catches the robber or stays in his current vertex (and never catches this robber).  Batman can only catch the robber if Batman's power is strictly greater than the robber's power.  Let's denote Batman's current vertex by $s$ ($s=s_i$ initially). To catch the robber, Batman must choose a vertex $g$ such that each vertex on the simple path between $x$ and $y$ (inclusive) lies also on the simple path between $s$ and $g$ (inclusive). If it is impossible to choose a vertex $g$ so that this condition is satisfied, Batman cannot catch this robber.  If Batman catches the robber, $g$ becomes his current vertex.  After Batman catches this robber, his power increases by $t$. Your task is to calculate the maximum power Batman can have after $M$ seconds if he chooses the robbers to catch optimally. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains three spaceseparated integers $N$, $s_i$ and $p$.  Each of the next $N1$ lines contains two spaceseparated integers $u$ and $v$ denoting an edge in the tree between vertices $u$ and $v$.  The next line contains a single integer $M$.  Each of the following $M$ lines contains four integers $x$, $y$, $r$ and $t$ describing a robber. ### Output For each test case, print a single line containing one integer — the maximum possible value of Batman's power after $M$ seconds. ### Constraints  $1 \le T \le 1,000$  $1 \le N, M \le 4\cdot10^5$  $1 \le s_i, u, v \le N$  the graph described on the input is a tree  $1 \le x, y \le N$  $x \neq y$  $1 \le p, r \le 10^9$  $t \le 10^9$  the sum of $N$ for all test cases does not exceed $4\cdot10^5$  the sum of $M$ for all test cases does not exceed $4\cdot10^5$ ### Example Input ``` 1 6 1 4 1 2 2 3 2 4 4 5 1 6 5 6 4 3 8 1 2 3 2 4 6 5 1 6 5 4 6 5 4 5 3 ``` ### Example Output ``` 12 ``` ### Explanation **Example case 1:**  In the 2nd second, Batman will go from vertex $s=1$ to vertex $g=5$.  In the 4th second, Batman will go from vertex $s=5$ to vertex $g=6$.  Batman should ignore robbers moving in the 1st, 3rd and 5th second.Author:  barenuz 
Editorial  https://discuss.codechef.com/problems/BTMNTREE 
Tags  barenuz, cook95, dynamicprogramming, hard, lazypropagation, segmenttree 
Date Added:  14062018 
Time Limit:  3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 