Mock CCC '15 S5 - Kongou

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 13.0s
Memory limit: 256M

Problem types
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 N (1 \le N \le 100\,000) 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 v from another sea u if there exists a sequence of seas S_1, S_2, \dots, S_n where n > 1, S_1 = u, S_n = v, and S_i is connected to S_{i+1} for 1 \le i < n.

You and your fleet girls are immortal, and so you get called upon to fight every few years, for a total of T (1 \le T \le 200\,000) 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 M (1 \le M \le 100\,000) 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 N, M, and T, each separated by a single space.

The second line of input will have T+1 numbers, A_0, A_1, \dots, A_T (1 \le A_i \le M). The i^\text{th} time you fight will be on the battlefield represented by the combination of formations A_{i-1} and A_i (1 \le i \le T).

The next M lines will contain descriptions of a single formation. Formation i (1 \le i \le M) on line i+2 will be described with 2K_i + 1 (1 \le K_i \le 200\,000) integers in this order: K_i\ x_{i, 1}\ y_{i, 1}\ \dots\ x_{i, K_i}\ y_{i, K_i}. For each pair (x_{i, j}, y_{i, j}) (1 \le j \le K_i, x_{i, j} \ne y_{i, j}, 1 \le x_{i, j}, y_{i, j} \le N), there is a river between lakes x_{i, j} and y_{i, j} in formation i.

It is guaranteed that the sum of all K_i will not exceed 200\,000.

Output Specification

The output should consist of T lines. Line i should have the number of critical rivers on the battlefield you fight on for the i^\text{th} time (1 \le i \le T).


For test cases worth at least 30% of the points, the additional constraints N, M, K_i, T \le 2\,000 will hold.

Sample Input 1

4 3 4
1 1 2 3 1
1 1 2
1 3 4
2 2 3 2 3

Sample Output 1


Sample Input 2

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


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.


  • 0
    account_disabled  commented on March 20, 2018, 9:24 a.m.

    What is the target time complexity?

    • 9
      koosaga  commented on March 21, 2018, 4:34 a.m. edited

      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)

  • -1
    heaven  commented on Nov. 10, 2015, 10:20 p.m.

    good job admirals