Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2017 /finals /tolls.md
wjomlex's picture
2017 Problems
7acee6b verified
A group of **N** Foxen reside in a peaceful forest community. Each Fox's
property consists of a tree stump as well as an underground den. There are
**N** \- 1 two-way paths on the ground running amongst the tree stumps, with
the _i_th path connecting the stumps belonging to two different Foxen **Ai**
and **Bi**, such that all **N** stumps can be reached from one another by
following a sequence of paths. Similarly, there are **N** \- 1 underground
tunnels running amongst the dens, with the _i_th tunnel connecting the dens
belonging to Foxen **Ci** and **Di**, such that all **N** dens can be reached
from one another. There's additionally a passageway connecting the tree stump
and den belonging to the 1st Fox, which is the only way in the whole forest to
get underground from the surface and vice versa.
At night the Foxen sleep in their dens, but during the daytime, they like to
emerge and relax lazily on their tree stumps. Each day, every Fox takes a trip
from their den to their tree stump, taking the unique shortest path through
the system of tunnels and paths to get there. However, this often requires
passing through other Foxen's properties, which they don't appreciate a whole
lot. To compensate, the Foxen have started charging each other tolls for said
passage. They don't have much of a currency, but Foxen do love crackers, so
those will do. Over a given period of **M** days, on the _i_th day, two
different Foxen **Wi** and **Xi** will each charge tolls for one of their
pieces of property. If **Yi** = "T", then Fox **Wi** will be charging tolls
for passage through their tree stump. Otherwise, if **Yi** = "D", then Fox
**Wi** will instead be charging tolls for passage through their den.
Similarly, Fox **Xi** will be charging tolls for passage through either their
tree stump (if **Zi**= "T") or their den (if **Zi**= "D").
Each day, whenever a Fox passes through another Fox's den or stump which is
subject to tolls on that day, they'll normally need to pay up with 2 crackers.
However, if they've already paid a toll earlier on that same trip, then the
property-owning Fox will take pity and only charge them 1 cracker instead of
2. As such, a Fox's daily trip may end up costing them at most 3 crackers. A
Fox will never charge themselves a toll, of course. If a pair of Foxen both
owe each other crackers, they'll still both pay up as normal, rather than
attempting to minimize the number of cracker transactions performed.
The Foxen are having some trouble keeping track of how many crackers they owe
one another. On each of the **M** days, they'd like to count up the total
number of crackers which will be charged as part of the tolls for the **N**
trips taken on that day. To avoid dealing with too many large numbers, they'd
like to combine these **M** cracker counts into a single value as follows
(where **Vi** is the _i_th day's count):
( ... (((**V1** * 12,345) + **V2**) * 12,345 + **V3**) ... * 12,345 + **VM**)
modulo 1,000,000,007
Please help the Foxen compute this combined value!
### Input
Input begins with an integer **T**, the number of different communities of
Foxen. For each community of Foxen, there is first a line containing the
space-separated integers **N** and **M**. Then **N - 1** lines follow, the
_i_th of which contains the space-separated integers **Ai** and **Bi**. Then
**N - 1** lines follow, the _i_th of which contains the space-separated
integers **Ci** and **Di**. Then **M** lines follow, the _i_th of which
contains the integers **Wi** and **Xi** and the characters **Yi** and **Zi**,
all separated by spaces.
### Output
For the _i_th community of Foxen, print a line containing "Case #**i**: "
followed by a single integer, the requested combined value based on the **M**
days' cracker counts, modulo 1,000,000,007.
### Constraints
1 ≤ **T** ≤ 30
2 ≤ **N** ≤ 500,000
1 ≤ **M** ≤ 500,000
1 ≤ **Ai**, **Bi**, **Ci**, **Di**, **Wi**, **Xi****N**
Both the sum of **N** values and the sum of **M** values across all **T**
cases do not exceed 1,500,000.
### Explanation of Sample
In the first case, Fox 1 doesn't need to pay any tolls to get from its den to
its tree stump, while Fox 2 must pay 2 crackers to complete its trip due to
passing through Fox's 1 tree stump.
In the second case, 5 crackers will be charged on the first day (the 3 Foxen
must pay 0, 2, and 3 crackers, respectively), 2 crackers will be charged on
the second day, and none will be charged on the third day. This results in a
final answer of (((5 * 12,345) + 2) * 12,345) + 0) modulo 1,000,000,007 =
762,019,815.
In the third case, 7, 6, and 4 crackers will be charged on each of the three
days, respectively.