2015 Mock CCC by Alex and Timothy
You are the admiral of an admirable fleet of battleships! Your flagship is the Kongou*, and you have never lost a single battle in your entire career. This is because you have adopted the foolproof strategy of blocking off all critical rivers before engaging in battle.
The battlefield can be thought of as
seas connected to each other by bidirectional rivers, each joining a pair of seas. No sea will be directly connected to itself by a river, but the same pair of seas can be connected by multiple rivers, and it may not be possible to draw the rivers on a map without some of them crossing each other. A river is considered critical if after blocking it, the pair of seas originally joined by it are no longer reachable from each other. Specifically, you can reach a sea
from another sea
if there exists a sequence of seas
where
,
,
, and
is connected to
for
.
You and your fleet girls are immortal, and so you get called upon to fight every few years, for a total of
times. The passage of time is not kind to the battlefield, and so the rivers that connect the seas change often (the seas themselves remain the same though). Normally, this would make it hard to quickly devise a plan for each battle, but there is still hope. In your experience, you have observed that there are
basic formations. A formation is simply a set of rivers, and each battlefield you fight on can be represented by combining two formations (see the sample input for a better understanding).
For each battlefield, you would like to determine the number of critical rivers.
*Kongou, based off of the Kongō.
Input Specification
The first line of input will have
,
, and
, each separated by a single space.
The second line of input will have
numbers,
. The
time you fight will be on the battlefield represented by the combination of formations
and
.
The next
lines will contain descriptions of a single formation. Formation
on line
will be described with
integers in this order:
. For each pair
, there is a river between lakes
and
in formation
.
It is guaranteed that the sum of all
will not exceed
.
Output Specification
The output should consist of
lines. Line
should have the number of critical rivers on the battlefield you fight on for the
time
.
Scoring
For test cases worth at least 30% of the points, the additional constraints
,
,
,
will hold.
Sample Input 1
Copy
4 3 4
1 1 2 3 1
1 1 2
1 3 4
2 2 3 2 3
Sample Output 1
Copy
0
2
1
1
Sample Input 2
Copy
7 3 3
1 2 3 1
4 1 2 1 3 5 7 6 7
4 2 3 3 4 4 5 5 6
3 1 4 4 7 2 6
Sample Output 2
Copy
2
2
2
Explanation for Sample Output 2
Originally, there are 7 seas numbered from 1 to 7.
The first formation looks like this:
The second formation looks like this:
The third formation looks like this:
In the first battle, the battlefield is made up of the first and second formations and looks like this:
The two critical rivers (in red) are 3 ↔ 4 and 4 ↔ 5.
In the second battle, the battlefield is made up of the second and third formations and looks like this:
The two critical rivers (in red) are 1 ↔ 4 and 4 ↔ 7.
In the third and final battle, the battlefield is made up of the third and first formations and looks like this:
The two critical rivers (in red) are 1 ↔ 3 and 5 ↔ 7.
Comments
What is the target time complexity?
nsqrtpolylog(n), with adequate constant factor.
Constant factor will be important in this problem imo. Better avoid slow-in-practice data structures (like link cut tree, which gives TLE for nsqrt queries in my case)
good job admirals