Planar Drawing

Given a combinatorial embedding, decide whether its boundary cycles satisfy n - m + f = 2, certifying that the embedding is planar.

Medium7GraphImplementationSimulationNo attempts yetTime limit1sMemory limit512 MB

Problem

This problem considers only finite, simple, undirected graphs that are connected. A planar graph is a graph that can be drawn in the plane so that no two edges cross, that is, edges meet only at shared end vertices. Such a drawing is a planar drawing of the graph. Figure 1 shows a planar graph and two of its planar drawings. Every planar graph is known to have a planar drawing in which all edges are straight line segments that do not intersect.

Figure 1. A planar graph and its planar drawings: (a) a complete graph with four vertices, (b) a planar drawing with curved edges, (c) a planar straight line drawing.

Testing whether a given graph is planar has been studied for a long time, and most known algorithms run in time linear in the number of vertices. A user of a planarity testing program sometimes wants more than a yes or no answer. Programmers make mistakes while implementing algorithms that were proven correct, and this is where certifying algorithms start.

When the user gives XX as an input and the program outputs YY, the user usually has no way of knowing whether YY is a correct output on input XX or a value broken by a bug. A certifying algorithm produces, with each output, a certificate ZZ that the particular output has not been compromised by a bug. By inspecting the certificate by hand or with a program, the user becomes convinced that the output is correct, or rejects the output as buggy. Checking ZZ is automated with a checker, an algorithm that verifies that ZZ proves YY to be a correct output for XX.

Consider the certificates a certifying algorithm for planarity testing should produce. If the input graph is planar, a planar drawing is the obvious certificate. If the graph is not planar, Kuratowski's theorem applies: a graph is planar if and only if it has no subgraph that is a subdivision of K5K_5 or of K3,3K_{3,3}. Here K5K_5 in Figure 2(a) is the complete graph with five vertices, and K3,3K_{3,3} in Figure 2(b) is the complete bipartite graph with three vertices in each bipartition set. A subdivision of a graph is obtained by repeatedly subdividing edges with new vertices of degree 2, as in Figures 2(c) and 2(d). That is, a subdivision replaces the edges of the graph with paths between their end vertices so that no path has an inner vertex on another path or in the graph. So if a graph is not planar, a connected subgraph that is a subdivision of K5K_5 or of K3,3K_{3,3} is a good certificate.

Figure 2. K5K_5, K3,3K_{3,3}, and their subdivisions: (a) K5K_5, (b) K3,3K_{3,3}, (c) a subdivision of K5K_5, (d) a subdivision of K3,3K_{3,3}.

Unlike checking whether a graph is a subdivision of K5K_5 or of K3,3K_{3,3}, checking whether a drawing is really planar is no easy task. So a combinatorial embedding, defined below, is adopted as the certificate for the affirmative case instead of a planar drawing. A straight line drawing in which no three vertices are collinear fixes, for each vertex vv, the cyclic order of the vertices adjacent to vv. Clockwise order is used here. The set of all these cyclic orders is a combinatorial embedding. Figure 3 shows a planar drawing and its corresponding combinatorial embedding. In the cyclic order (2,3,4)(2,3,4), for instance, the vertex after 2 is 3, the vertex after 3 is 4, and the vertex after 4 is 2.

Figure 3. A planar drawing and its combinatorial embedding: (a) a planar drawing, (b) its combinatorial embedding, (c) the four boundary cycles of the combinatorial embedding.

To check whether a combinatorial embedding is really planar, the notion of a boundary cycle is needed. Replace each undirected edge by two directed edges in opposite directions. The boundary cycle starting at a directed edge (u,v)(u, v) is defined as follows. If uu' is the vertex next to uu in the cyclic order for vv, then (v,u)(v, u') is the next edge of the boundary cycle. Continue this way until the starting edge comes back. In the directed graph of Figure 3(c), starting at (5,4)(5,4), the cyclic order for vertex 4 is (5,2,1,3)(5,2,1,3) and the vertex next to 5 is 2, so the next edge is (4,2)(4,2). Continuing gives the boundary cycle (5,4)(4,2)(2,3)(3,4)(4,5)(5,4)(5,4) \to (4,2) \to (2,3) \to (3,4) \to (4,5) \to (5,4).

A combinatorial embedding always partitions the set of directed edges into boundary cycles. Moreover, for a connected graph with n>1n > 1 vertices and mm edges, a combinatorial embedding of the graph with ff boundary cycles is planar if and only if nm+f=2n - m + f = 2. Both facts have been proven. It therefore suffices to count the boundary cycles and check that equation.

Suppose an input graph XX is given, together with the output YY and the certificate ZZ of a certifying algorithm for planarity testing. Here YY is a yes or no answer, and ZZ is a combinatorial embedding or a subgraph of XX depending on the answer. A checker program splits into two modules. The first module tests whether the combinatorial embedding ZZ is planar in the affirmative case, and whether the subgraph ZZ is a subdivision of K5K_5 or of K3,3K_{3,3} in the negative case. The second module tests whether ZZ is a combinatorial embedding of XX or a subgraph of XX, again depending on the answer. Write the first module. The vertices of the graph XX are indexed 1 to nn.

Input

The first line contains an integer indicating the output of a certifying algorithm, 1 in the affirmative case and -1 in the negative case. The second line contains nn, the number of vertices of the graph XX (2n50002 \le n \le 5000).

In the affirmative case, nn more lines describe the combinatorial embedding. Line kk contains dkd_k followed by vertex numbers x1,x2,,xdkx_1, x_2, \dots, x_{d_k}, representing the cyclic order (x1,x2,,xdk)(x_1, x_2, \dots, x_{d_k}) of vertex kk. These nn lines describe a combinatorial embedding of a connected simple graph on nn vertices: no number appears twice in one line, and uu appears in the list of vv exactly when vv appears in the list of uu. The number of edges mm of this graph satisfies 1m500001 \le m \le 50000.

In the negative case, one line contains nn' and mm', the numbers of vertices and edges of the subgraph (1nn1 \le n' \le n, 1m500001 \le m' \le 50000), and mm' lines follow, each containing two integers uu and vv that represent an edge between vertex uu and vertex vv. Every vertex number lies in {1,,n}\{1, \dots, n\}. The certificate may be buggy: the same edge may be listed twice, an edge may join a vertex to itself, the graph may be disconnected, and the number of distinct vertices appearing in the list may differ from nn'.

Output

Print exactly one line.

In the affirmative case, print 1 if the given combinatorial embedding is planar, and -1 otherwise. That is, with mm the number of edges and ff the number of boundary cycles, print 1 if nm+f=2n - m + f = 2 and -1 otherwise.

In the negative case, print 1 if the given mm' edges form a subdivision of K5K_5 or of K3,3K_{3,3}, and -1 otherwise. The subgraph counts as such a subdivision only when all of the following hold. No edge is listed twice. No edge joins a vertex to itself. The number of distinct vertices in the edge list equals nn'. The graph is connected. Every vertex has degree at least 2. Replacing each maximal path whose inner vertices all have degree 2 by a single edge yields exactly K5K_5 or exactly K3,3K_{3,3}. If that replacement produces a loop or two parallel edges, the subgraph is not a subdivision.