Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2014 /finals /tours.html
wjomlex's picture
2014 Problems
718fd53 verified
<p>
Facebook HQ -- a mysterious place full of magical code and trade secrets.
If outsiders were ever to breach the walls of the compound, which are
protected by a legion of security foxes, the entire company could well
be brought to its knees!
</p>
<p>
Hmmm. Actually, campus tours are given regularly.
</p>
<p>
The compound consists of <b>N</b> buildings, with <b>M</b> walkways running amongst them.
The <i>i</i>th walkway connects buildings <b>A<sub>i</sub></b> and <b>B<sub>i</sub></b>, (<b>A<sub>i</sub></b> != <b>B<sub>i</sub></b>)
and no two buildings are
directly connected by more than one walkway. There are no other ways to
move from building to building.
</p>
<p>
Over a period of <b>D</b> days, some events will occur at Facebook HQ. One
of two types of events will happen on the <i>i</i>th day, indicated by a character
<b>E<sub>i</sub></b>. If <b>E<sub>i</sub></b> = 'T', then a tour will take place. Otherwise, <b>E<sub>i</sub></b> = 'S', and a
security sweep of one building will take place.
</p>
<p>
If a tour is given on the <i>i</i>th day, visitors will plan to enter the compound at
building <b>X<sub>i</sub></b>, and leave from building <b>Y<sub>i</sub></b> (<b>X<sub>i</sub></b> != <b>Y<sub>i</sub></b>). If it turns out that
these two buildings are not actually connected by any sequence of walkways,
then the tour will be cancelled, and the unfortunate visitors will be
given Facebook T-shirts on the way out. Otherwise, a large number of people
will be led from building <b>X<sub>i</sub></b> to building <b>Y<sub>i</sub></b> along various routes. No route
will involve travelling along the same walkway multiple times (even in
different directions), but a route might revisit the same building repeatedly,
including buildings <b>X<sub>i</sub></b> and <b>Y<sub>i</sub></b>. Along the way some visitors will inevitably
get themselves "lost", and fail to rejoin the tour group. In total, <b>O<sub>i</sub></b> new
outsiders will be left behind in each building which could possibly be part of
any valid tour route from building <b>X<sub>i</sub></b> and building <b>Y<sub>i</sub></b>. Good thing they'll no
doubt have brought cameras to amuse themselves with while they wait to be
found.
</p>
<p>
On the other hand, if a security sweep is conducted on the <i>i</i>th day, then the
security foxes will carefully search building <b>Z<sub>i</sub></b> for any trespassers
remaining from previous tours, and kindly escort them out.
</p>
<p>
Since Facebook likes data, you've been hired to record how many outsiders
were found in each sweep.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <b>T</b>, the number of test cases. Each test case
begins with a line containing three integers, <b>N</b>, <b>M</b>, and <b>D</b>.
The next <b>M</b> lines contain two integers <b>A<sub>i</sub></b> and <b>B<sub>i</sub></b>.
The next <b>D</b> lines contain a character <b>E<sub>i</sub></b>, followed by either three integers
<b>X<sub>i</sub></b>, <b>Y<sub>i</sub></b>, <b>O<sub>i</sub></b> if <b>E<sub>i</sub></b> = 'T', or a single integer <b>Z<sub>i</sub></b> if <b>E<sub>i</sub></b> = 'S'.
</p>
<h3>Output</h3>
<p>
For each test case <i>i</i>, output "Case #i: " followed by the total number of visitors the foxes escort off the campus. Since this number may be quite large, output it modulo 1,000,000,007.
</p>
<h3>Constraints</h3>
<p>
1 &le; <b>T</b> &le; 20 <br/>
1 &le; <b>N</b> &le; 10<sup>5</sup> <br/>
1 &le; <b>M</b> &le; 10<sup>6</sup> <br/>
1 &le; <b>D</b> &le; 10<sup>6</sup> <br/>
1 &le; <b>O<sub>i</sub></b> &le; 1,000 <br/>
1 &le; <b>A<sub>i</sub></b>, <b>B<sub>i</sub></b>, <b>X<sub>i</sub></b>, <b>Y<sub>i</sub></b>, <b>Z<sub>i</sub></b> &le; <b>N</b> <br/>
</p>
<h3>Explanation of Sample</h3>
<p>
In the first sample case:
</p>
<p>
On the first day, a tour is given from building 1 to building 2. The only
valid route consists of simply crossing the walkway between these two
buildings. As such, by the end of the day, 5 outsiders are left hiding in each
of buildings 1 and 2.
</p>
<p>
On the second day, the tour cannot take place.
</p>
<p>
On the third and fourth days, security sweeps of buildings 2 and 6 are carried
out, with 5 and 0 outsiders found respectively.
</p>
<p>
On the fifth day, a tour is given from building 2 to building 3. There are
exactly three valid routes (2, 3), (2, 3, 4, 5, 3), (2, 3, 5, 4, 3). As such,
one new outsider remains behind in each of buildings 2, 3, 4, and 5.
</p>
<p>
On the sixth day, the valid tour routes are (5, 3) and (5, 4, 3), so 14 new
outsiders take up residence in each of buildings 3, 4, and 5.
</p>
<p>
Finally, security sweeps of buildings 1, 2, and 4 are conducted evicting 5, 1,
and 15 people respectively, for a grand total of 26.
</p>