Under construction

Still being built. Things may change

Railroad Maintenance

Interview

Time limit40sMemory limit1024 MB

Summary
Count the train lines whose removal makes some pair of stations unreachable from each other, given the stations each line serves.
Level

Medium5 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

You are in charge of the maintenance of a railroad network. The network has NN stations and LL 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 aa to station bb is possible if there is a list of train lines such that the first line serves aa, the last line serves bb, 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 TT. Each test case starts with a line containing two integers NN and LL, the number of stations and train lines.

Then LL groups follow, each taking two lines. The first line of the ii-th group contains KiK_i, the number of stations served by the ii-th train line. The second line contains KiK_i integers Si,1,Si,2,…,Si,KiS_{i,1}, S_{i,2}, \dots, S_{i,K_i}, the stations served by that line.

Output

For each test case, output one line in the format Case #x: y, where xx is the test case number starting from 1 and yy is the number of essential train lines.

Limits

  • 1≤T≤1001 \le T \le 100.
  • 2≤Ki≤N2 \le K_i \le N for all ii.
  • 1≤Si,j≤N1 \le S_{i,j} \le N for all i,ji, j.
  • Si,j≠Si,j′S_{i,j} \ne S_{i,j'} for all i,j,j′i, j, j' with j≠j′j \ne j'. 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 22. 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 11 to station 44 without it. As in Sample Case #1, this line already connects every station, so no other line is essential.

Examples1

  1. Example 1

    Input
    4
    4 3
    3
    1 2 3
    2
    1 4
    3
    4 1 3
    4 4
    2
    1 2
    2
    3 4
    2
    3 2
    2
    4 1
    4 3
    2
    1 2
    2
    3 4
    2
    3 2
    4 3
    2
    1 2
    2
    3 4
    4
    4 1 2 3
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: 3
    Case #4: 1