Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2015 /round3 /fox_rocks.html
wjomlex's picture
2015 Problems
761e24d verified
raw
history blame
5.38 kB
<p>
Mr. Fox sure loves his rocks! In fact, when he's not in a hurry, he often looks at the rocks lying around near him to decide where to wander in his forest.
</p>
<p>
Mr. Fox lives in a forest with <strong>N</strong> clearings, numbered from 0 to <strong>N</strong>-1,
with <strong>P</strong> one-way trails initially running amongst them.
The <strong>i</strong>th trail runs from clearing <strong>A<sub>i</sub></strong>
to a different clearing <strong>B<sub>i</sub></strong>,
and is littered with <strong>R<sub>i</sub></strong> rocks.
No two clearings are connected by multiple trails running in the same direction,
though they could be connected by 2 trails running in opposite directions.
Additionally, an interesting property of this forest is that a trail from clearing
<strong>a</strong> to clearing <strong>b</strong> may only exist if
0 &le; floor(<strong>b</strong>/4) - floor(<strong>a</strong>/4) &le; 1.
</p>
<p>
To entertain himself over a period of <strong>D</strong> days, Mr. Fox will cause one event to occur on each day.
The <strong>i</strong>th event may be one of 3 types, determined by the value of <strong>E<sub>i</sub></strong>:
<ol>
<li><p>
Given 3 integers <strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong>, and <strong>Z<sub>i</sub></strong>,
Mr. Fox will create a new trail from clearing <strong>X<sub>i</sub></strong> to a different clearing
<strong>Y<sub>i</sub></strong>, and drop <strong>Z<sub>i</sub></strong> rocks onto it.
It's guaranteed that no trail from <strong>X<sub>i</sub></strong> to <strong>Y<sub>i</sub></strong>
will exist at the start of the <strong>i</strong>th day, and that
0 &le; floor(<strong>Y<sub>i</sub></strong>/4) - floor(<strong>X<sub>i</sub></strong>/4) &le; 1 will hold.
</p></li>
<li><p>
Given 2 distinct integers <strong>X<sub>i</sub></strong> and <strong>Y<sub>i</sub></strong>,
Mr. Fox will completely destroy the trail from clearing
<strong>X<sub>i</sub></strong> to clearing <strong>Y<sub>i</sub></strong>
(which is guaranteed to exist at the start of the <strong>i</strong>th day). Note that, once such a trail is destroyed,
a new trail from <strong>X<sub>i</sub></strong> to <strong>Y<sub>i</sub></strong> may be created in the future.
</p></li>
<li><p>
Given 2 distinct integers <strong>X<sub>i</sub></strong> and <strong>Y<sub>i</sub></strong>,
Mr. Fox will take a "random stroll" starting at clearing <strong>X<sub>i</sub></strong>,
and would like to determine the probability that he'll visit clearing <strong>Y<sub>i</sub></strong> at least once during it.
</p>
<p>
A "random stroll" consists of repeating the following process potentially infinitely:
If Mr. Fox is currently in some clearing <strong>c</strong>, and there are no outgoing trails from <strong>c</strong>,
then the stroll ends immediately.
Otherwise, he'll consider all of the rocks on all of the outgoing trails from <strong>c</strong>,
choose one of these rocks uniformly at random, follow the trail on which that rock lies to its destination clearing
(without removing any rocks), and repeat the process from his new clearing.
</p></li>
</ol>
<p>
For each event of type 3, output the requested probability.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of test cases.
For each test case, there is first a line containing the space-separated integers
<strong>N</strong>, <strong>P</strong>, and <strong>D</strong>.
</p>
<p>
Then, <strong>P</strong> lines follow, the <strong>i</strong>th of which contains the space-separated integers
<strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, and <strong>R<sub>i</sub></strong>.
</p>
<p>
Then, <strong>D</strong> lines follow, the <strong>i</strong>th of which contains the space-separated integers
<strong>E<sub>i</sub></strong>, <strong>X<sub>i</sub></strong>, and <strong>Y<sub>i</sub></strong>.
If <strong>E<sub>i</sub></strong> = 1, this line additionally contains the integer <strong>Z<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <strong>i</strong>th test case, print a line containing "Case #<strong>i</strong>: "
followed by the computed probabilities for each stroll that Mr. Fox takes.
These probabilities should be space-separated, and rounded to 6 decimal places.
</p>
<p>
Absolute errors of up to 2 * 10<sup>-6</sup> will be ignored.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 20 <br />
1 &le; <strong>N</strong> &le; 50,000 <br />
0 &le; <strong>P</strong> &le; 100,000 <br />
1 &le; <strong>D</strong> &le; 20,000 <br />
0 &le; <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>,
<strong>X<sub>i</sub></strong>, <strong>Y<sub>i</sub></strong> &lt; <strong>N</strong> <br />
1 &le; <strong>E<sub>i</sub></strong> &le; 3 <br />
1 &le; <strong>R<sub>i</sub></strong>, <strong>Z<sub>i</sub></strong> &le; 5 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first test case, Mr. Fox does multiple strolls from clearing 0 while looking out for clearing 3.
His first stroll has probability 1 as he must always end up at clearing 3.
His second stroll has probability 1/2 as there's a 50% chance he gets stuck in clearing 4.
His third stroll has probability 2/3 as he now only goes to clearing 4 1/3 of the time.
His fourth stroll has probability 1/3 as he gets stuck in clearing 4 1/3 of the time, and in clearing 1 1/3 of the time.
</p>