2015 Mock CCC by Alex and Timothy

Problem S5: Kongou

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 ≤ N ≤ 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 S1, S2, …, Sn where n > 1, S1 = u, Sn = v, and Si is connected to Si + 1 for 1 ≤ 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 ≤ T ≤ 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 ≤ M ≤ 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 Format

The first line of input will contain the integers N, M, and T, each separated by a single space.

The second line of input will have T + 1 numbers, A0, A1, …, AT (1 ≤ AiM). The i-th time you fight will be represented by the combination of formations Ai−1 and Ai (for 1 ≤ iT).

The next M lines will contain descriptions of a single formation. Formation i (1 ≤ iM) on line i + 2 will be described with 2Ki + 1 (1 ≤ Ki ≤ 200 000) integers in this order: Kixi,1yi,1 … xi,Kiyi,Ki. For all 1 ≤ jKi, there is a river between lakes xi,j and yi,j in formation i (where xi,jyi,j and 1 ≤ xi,j, yi,jN).

It is guaranteed that the sum of all Ki will not exceed 200 000.

Output Format

The output shoud consist of T lines. Line i should have the number of critical rivers on the battlefield you fight on for the i−th time (1 ≤ iT).


For test cases worth at least 30% of the points, the additional constraints N, M, Ki, T ≤ 2000 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 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.

All Submissions
Best Solutions

Point Value: 40 (partial)
Time Limit: 20.00s
Memory Limit: 256M
Added: Feb 18, 2015
Authors: FatalEagle, Alex

Languages Allowed:

Comments (Search)

It's quiet in here...