Galactic Breakup
Time limit1sMemory limit128 MB
Given three dimensional grid cell labels and the cell lists of the monarchies seceding in a fixed order, count the months after which the remaining cells form two or more pieces.
- Level
Medium7 of 10
- Topics
- Union-find, Graph, Simulation
- Solved
- No attempts yet
Problem
After ruling a large chunk of the galaxy for millennia, a vast empire is finally breaking up into a collection of independent monarchies. The empire is very organized and takes the shape of a gigantic cube with dimensions by by parsecs. (Only a few know the exact values of , , and .) To facilitate control, it is partitioned into smaller dominions, each exactly cubic parsec in size.
The dominions are numbered as follows. The dominion at coordinates (, , ) has number
so the numbers run from to , with increasing fastest, then , then . Two dominions are neighbors (share a face) exactly when precisely one of their three coordinates differs by and the other two are equal.
The empire is divided into independent monarchies. Each monarchy is a connected set of one or more dominions (connected through shared faces), and the monarchies together partition all dominions. Over a period of several months, exactly one monarchy per month secedes from the empire, in the given order (monarchy 1 first, then 2, and so on). On the first day of month , monarchy leaves the empire. After each secession, the remaining empire is the union of the monarchies that have not yet seceded.
Determine, over the course of the breakup, the number of months during which the remaining empire is disconnected (that is, its dominions form two or more separate connected pieces). A remaining empire that is empty or forms a single piece counts as connected.
Input
The first line contains the number of test cases .
Each test case begins with a line of four integers (; is the number of monarchies). The following lines describe the monarchies in the order in which they secede. Each has the form , where () is the number of dominions in the monarchy and are their numbers. The monarchies partition all dominions.
Output
For each test case, print a single line containing one integer: the number of months during which the remaining empire was disconnected.