This page is still under construction.

Parts of this page are still being built. What you see may change.

Farmland

Time limit1sMemory limit128 MB

Summary
Given a planar graph of farming regions, count the proper regions bounded by a simple cycle with no interior vertices or edges and exactly k boundary edges.
Level

Hard8 of 10

Topics
Graph, Geometry, Implementation, DFS
Solved
No attempts yet

Problem

You are given the farmland map of a country. The whole farmland is divided into a set of disjoint farming regions, and each farmer owns exactly one region. There is a boundary fence between two neighboring regions. The map can be represented as a plane graph G(V,E)G(V, E).

There are two kinds of edges: a boundary edge, which lies between two neighboring regions, and a non-boundary edge, which sticks into the interior of a region.

A proper farming region is a closed region bounded by a single simple cycle that contains no vertex or edge in its interior. For example, if a quadrilateral has another vertex inside it, that quadrilateral is not a proper region. A region whose bounding cycle is not simple (it repeats a vertex or an edge) is not proper either. A degenerate region with no interior area (for example, one made of only two vertices) is also not proper.

Assume the following about G(V,E)G(V, E):

  • The graph is simple and connected: there are no self-loops and no parallel edges.
  • The outer (unbounded) face of G(V,E)G(V, E) is never counted.
  • There is at least one proper farming region.
  • All vertex positions are distinct.
  • No two edges cross, so G(V,E)G(V, E) is a plane graph.

The size of a proper farming region is the number of boundary edges around it. For example, a quadrilateral region bounded by four edges has size 44.

Given an integer kk, count how many proper farming regions have size exactly kk. If there is no such region, print 00.

Input

The first line contains the number of test cases MM (1≤M<101 \le M < 10).

Then MM test cases follow. The first line of each test case contains the number of vertices NN (3≤N<2003 \le N < 200).

Each of the next NN lines describes one vertex in the form:

i xi yi di a1 a2 ... adi

Here ii is the vertex number, (xi,yi)(x_i, y_i) is the coordinate of vertex ii, did_i is the degree of vertex ii, and a1,…,adia_1, \dots, a_{d_i} are the vertices adjacent to ii.

The last line of each test case contains kk, the size of the proper regions you must count.

All vertices lie on grid points of a 1000×10001000 \times 1000 lattice.

Output

For each test case, print on its own line the number of proper farming regions whose size is exactly kk. In other words, print MM non-negative integers.

Examples3

  1. Example 1

    Input
    2
    12
    1  2 6   3  9 7 2
    2  5 6   4  5 3 1 8
    3  3 5   2  4 2
    4  3 4   2  3 5 
    5  4 4   2  4 2
    6  7 4   1  8
    7  2 3   2  8 1
    8  5 3   5  7 2 9 12 6
    9  1 2   3  11 8 1
    10 3 2   1  11
    11 2 1   3  10 9 12
    12 6 1   2  8 11
    4
    3
    1  2 2   2  2 3
    2  1 1   2  1 3
    3  4 1   2  1 2
    4
    
    Expected output
    2
    0
    
  2. Example 2

    Input
    1
    3
    1 0 0 2 2 3
    2 2 0 2 1 3
    3 1 2 2 1 2
    3
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    4
    1 0 0 2 2 4
    2 2 0 2 1 3
    3 2 2 2 2 4
    4 0 2 2 3 1
    4
    
    Expected output
    1