Double Distance Query
All submissions for this problem are available.### Read problem statements in [Hindi](http://www.codechef.com/download/translated/LTIME77/hindi/DDQUERY.pdf), [Bengali](http://www.codechef.com/download/translated/LTIME77/bengali/DDQUERY.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/LTIME77/mandarin/DDQUERY.pdf), [Russian](http://www.codechef.com/download/translated/LTIME77/russian/DDQUERY.pdf), and [Vietnamese](http://www.codechef.com/download/translated/LTIME77/vietnamese/DDQUERY.pdf) as well. You are given an unweighted tree with $N$ nodes (numbered $1$ through $N$). Let's denote the distance between any two nodes $p$ and $q$ by $d(p, q)$. You should answer $Q$ queries. In each query, you are given parameters $a$, $d_a$, $b$, $d_b$, and you should find a node $x$ such that $d(x, a) = d_a$ and $d(x, b) = d_b$, or determine that there is no such node. ### 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 two space-separated integers $N$ and $Q$. - Each of the next $N-1$ lines contains two space-separated integers $u$ and $v$ denoting that nodes $u$ and $v$ are connected by an edge. - Each of the next $Q$ lines contains four space-separated integers $a$, $d_a$, $b$ and $d_b$ describing a query. ### Output For each query, print a single line containing one integer ― the number of a node satisfying the given requirements, or $-1$ if no such node exists. If there are multiple solutions, you may output any one. ### Constraints - $1 \le T \le 1,000$ - $1 \le N, Q \le 10^6$ - $1 \le u, v \le N$ - the graph on the input is a tree - $1 \le a, b \le N$ - $1 \le d_a, d_b \lt N$ - the sum of $N$ over all test cases does not exceed $10^6$ - the sum of $Q$ over all test cases does not exceed $10^6$ ### Subtasks **Subtask #1 (50 points)**: - $1 \le N, Q \le 1,000$ - the sum of $N$ over all test cases does not exceed $1,000$ - the sum of $Q$ over all test cases does not exceed $1,000$ **Subtask #2 (50 points)**: original constraints ### Example Input ``` 1 5 3 1 2 2 3 3 4 3 5 2 1 4 1 2 2 4 2 1 1 2 1 ``` ### Example Output ``` 3 5 -1 ```
|Tags||jafarbadour, jafarbadour, ltime77|
|Time Limit:||6 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, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.