Railroad Maintenance
InterviewTime limit40sMemory limit1024 MB
Count the train lines whose removal makes some pair of stations unreachable from each other, given the stations each line serves.
Problem
You are in charge of the maintenance of a railroad network. The network has stations and train lines. Each train line serves a fixed list of stations and runs in both directions (trains turn around at the first and last stations of the list). Passengers can transfer between lines at a shared station. A trip from station to station is possible if there is a list of train lines such that the first line serves , the last line serves , and every pair of consecutive lines in the list shares at least one station.
Shutting down entire lines is the easiest way to do maintenance, one line at a time. A train line is essential if removing it makes at least one trip between a pair of stations impossible.
Given the existing train lines, count how many of them are essential.
Input
The first line contains the number of test cases . Each test case starts with a line containing two integers and , the number of stations and train lines.
Then groups follow, each taking two lines. The first line of the -th group contains , the number of stations served by the -th train line. The second line contains integers , the stations served by that line.
Output
For each test case, output one line in the format Case #x: y, where is the test case number starting from 1 and is the number of essential train lines.
Limits
- .
- for all .
- for all .
- for all with . Each train line serves a station at most once.
- With no train line shut down, a trip between every pair of stations is possible.
Hint
In Sample Case #1, the first train line is essential because it is the only line serving station . Shutting down any other line does not make a trip between any pair of stations impossible, so they are not essential.

In Sample Case #2, no line is essential.

Sample Case #3 is like Sample Case #2, but the last train line is missing. That makes every remaining train line essential.
In Sample Case #4, the last train line is essential because there is no way to go from station to station without it. As in Sample Case #1, this line already connects every station, so no other line is essential.
