Railroad Maintenance

시간 제한40초메모리 제한1024 MB

요약
역과 노선의 이분 그래프에서 다리 역할을 하는 노선의 수를 센다.
난이도

어려움10점 중 8점

유형
그래프, DFS, 유니온 파인드
정답자
아직 제출이 없습니다

문제

You are in charge of the maintenance of a railroad network. The network consists of N\mathbf{N} stations and L\mathbf{L} train lines. Each train line serves a fixed list of stations bidirectionally (trains turn around in the first and last stations of the list). Transfers from one line to another in a station are possible, which means a trip in the network from station aa to station bb is possible if there is a list of train lines such that the first one serves station aa, the last one serves station bb, and for any consecutive pair of train lines in the list there is at least one station that they both serve.

The easiest way to do maintenance is to shut down entire lines, one at a time. However, some train lines may be essential. A train line is essential if removing it would make at least one trip between a pair of stations not possible.

Given the list of existing train lines, calculate how many of them are essential.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing two integers N\mathbf{N} and L\mathbf{L}: the number of stations and train lines in the network. Then, L\mathbf{L} groups of 2 lines follow. The first line of the ii-th group contains a single integer K_i\mathbf{K\_i} the number of stations served by the ii-th train line. The second line of the ii-th group contains K_i\mathbf{K\_i} integers S_i,1,S_i,2,…,S_i,K_i\mathbf{S\_{i,1}}, \mathbf{S\_{i,2}}, \dots, \mathbf{S\_{i,{K\_i}}} representing the stations served by the ii-th train line.

출력

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

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • 2≤K_i≤N2 \le \mathbf{K\_i} \le \mathbf{N} for all ii.
  • 1≤S_i,j≤N1 \le \mathbf{S\_{i,j}} \le \mathbf{N}, for all i,ji, j.
  • S_i,j≠S_i,j′\mathbf{S\_{i,j}} \ne \mathbf{S\_{i,j'}}, for all i,j,j′i, j, j' such that j≠j′j \ne j' (Each train line serves a station at most once).
  • The trip between all pairs of stations is possible as per the definition above when no train line is shut down.

힌트

In Sample Case #1, the first train line is essential because it is the only one serving station 22. Since shutting any other line down would not make travel between at least one pair of stations impossible, they are not essential.

In Sample Case #2, no line is essential.

Sample Case #3 is similar to Sample Case #2, but missing the last train line. That makes all remaining train lines essential.

In Sample Case #4, the last train line is essential as there is no way to go from station 11 to station 44 without it. As in Sample Case #1, since this train line already connects every station, no other line is essential.

예제1

  1. 예제 1

    입력
    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
    
    예상 출력
    Case #1: 1
    Case #2: 0
    Case #3: 3
    Case #4: 1