Submit | All submissions | Best solutions | Back to list |
WPC5H - Nymphs from H10 |
A far away galaxy, H10 has newly been discovered and incorporated into the Commonwealth which participates in the Galactic Wars. The other galaxies have always bullied it by winning the Galactic Wars every year. Frustrated, H10 has come up with a novel strategy to distract the other galaxies. They are planning to send beautiful nymphs into the lands of the other galaxies.
The cosmos is arranged so that there is a unique path between any two galaxies (possibly going through other galaxies). The leaders in H10 will select two different galaxies A and B, and send K nymphs to each of the galaxies that is on the unique path from A to B. Initially there are no nymphs on any galaxy.
You are the Chief Executive Officer of this project. The leaders will ask you to send these groups of nymphs by giving various instructions of the form (A, B, K). In the process, they may also ask you how many nymphs are currently there in particular galaxies, so that they are able to take smart decisions in the future. Implement this. (See exact format in Input/Output Specifications)
Input
The first line contains t, the number of test cases.
T test cases follow. Each contains:
A line containing n, the number of galaxies (numbered from 0 to n-1).
n-1 lines describing the connections between 2 galaxies as 2 space separated integers a, b (both from 0 to n-1), denoting a direct connection between galaxy a, and galaxy b.
The next line contains a single integer Q denoting the number of queries.
Next Q lines contain queries in the form of 3 space separated integers a, b and k.
Output
For each query if k>=0, don't output anything, just send the nymphs to every galaxy on the way.
In a query if k=-1, the output 2 space separated integers in a new line denoting the number of nymphs in galaxy a, and galaxy b respectively.
Constraints
1 <= T <= 10
2 <= n <= 50,000
The galaxies are connected in such a way that there is a unique path from each galaxy to every other galaxy.
2 <= Q <= 50,000
0 <= a, b <= n-1
a and b are different
-1 <= k <= 100
Example
Input: 1 3 0 1 1 2 2 1 2 2 0 2 -1 Output: 0 2
Explanation
The path from 1 to 2 is 1 -> 2. 2 nymphs are thus sent to galaxies 1 and 2. There were 0 nymphs at galaxy 0 initially.
Added by: | triveni |
Date: | 2014-03-29 |
Time limit: | 1.146s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM32-GCC ASM64 MAWK BC C-CLANG NCSHARP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET |
Resource: | ACA judge IITK, WPC5 |
hide comments
2014-04-02 14:33:50 Jacob Plachta
In the queries, a can be equal to b (both for k=-1, and k>=0)! If a=b and k>=0, k nymphs are added to just node a. Last edit: 2014-04-02 15:04:23 |