Under construction

Still being built. Things may change

Cactus Meets Torus

Time limit3sMemory limit1024 MB

Summary
Given a cactus as edge-disjoint paths, decide if it can be drawn on a torus so that cutting along any one cycle keeps the sheet in one piece.
Level

Hard8 of 10

Topics
Graph, DFS, Math
Solved
No attempts yet

Problem

Alice has a nice cactus graph that she wants to place on a sheet of paper. Eve threatens to take one cycle of this cactus and cut the paper along all edges on this cycle. The sheet of paper would then be divided into two parts, and Alice would be upset. Luckily, Barbara just gave Alice a paper torus: a sheet of paper whose top and bottom edges are connected, and whose left and right edges are connected, without twisting. On a torus, you can sometimes cut paper along all edges of a cycle and it still remains in one piece. Help Alice determine whether she can place her cactus on a torus so that Eve cannot cut paper along one cycle to divide the torus into two unconnected pieces.

A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair of vertices) and loops (edges that connect a vertex to itself) are not allowed in a cactus.

A graph is placed on a sheet of paper if each vertex is a point on the sheet, each edge is a segment between the points of its vertices, and these segments intersect only at their ends. On a torus, segments may go through the sheet edges any number of times.

Input

The input consists of one or more independent test cases.

The first line of each test case contains two integers nn and mm (1≤n≤1051 \le n \le 10^5; 0≤m≤1050 \le m \le 10^5), where nn is the number of vertices in the graph. Vertices are numbered from 11 to nn. The edges of the graph are given as a set of edge-distinct paths, where mm is the number of such paths.

Each of the following mm lines contains a path in the graph. A path starts with an integer sis_i (2≤si≤10002 \le s_i \le 1000) followed by sis_i integers from 11 to nn. These sis_i integers are the vertices of the path. Adjacent vertices in a path are distinct. A path may pass through the same vertex several times, but every edge is traversed exactly once in the whole test case. The graph has no multiedges, so there is at most one edge between any pair of vertices.

The last line of the input contains two zeros. It is not a test case and marks the end of the input, so it requires no output.

All graphs in the input are cacti. The sum of all values of nn and the sum of all values of mm over the whole input each do not exceed 10510^5.

Output

For each test case, print a single line with "Yes" if the cactus can be placed on a torus, or "No" otherwise.

Hint

One way to place the cactus from the first case on a torus is shown in the picture.

Examples1

  1. Example 1

    Input
    6 1
    8 1 2 3 1 4 5 6 4
    10 2
    9 1 2 3 1 10 4 5 6 4
    5 7 8 9 7 10
    0 0
    
    Expected output
    Yes
    No