Under construction

Still being built. Things may change

Grammar

Time limit2sMemory limit1024 MB

Summary
Given a context-free grammar over a and b, decide whether the language contains a string with more a characters than b characters.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Shortest path
Solved
No attempts yet

Problem

A formal grammar is a way of describing formal languages. It is written as Γ=⟨Σ,N,S∈N,P⊂N+×(Σ∪N)\*⟩\Gamma = \langle \Sigma, N, S \in N, P \subset N^{+} \times (\Sigma \cup N)^{\*}\rangle, where Σ\Sigma is an alphabet whose elements are called terminals. NN is the set of nonterminals, SS is the starting nonterminal, and PP is the set of production rules, each of the form α→β\alpha \rightarrow \beta.

Here, N+N^{+} is the set of all strings of one or more elements of NN (non-empty strings of nonterminals). (Σ∪N)\*(\Sigma\cup N)^{\*} is the set of all strings of zero or more elements of Σ∪N\Sigma\cup N, including the empty string.

A grammar is context-free if the left side of each rule is exactly one nonterminal. In other words, P⊂N×(Σ∪N)\*P \subset N \times (\Sigma\cup N)^{\*}.

For example, take the alphabet Σ={‘a‘,‘b‘}\Sigma = \{`a`, `b`\}, the nonterminals N={S,A}N = \{S, A\}, and these two rules:

  1. S→‘b‘AS \rightarrow `b`A
  2. A→‘aa‘A \rightarrow `aa`

This grammar is context-free.

To generate a language, start from the string consisting of only SS, then apply production rules one or more times. Applying a rule means finding its left side in the current string and replacing it with its right side. The language generated by the grammar is the set of all strings made only of terminals that can be produced this way.

In the example above, baabaa belongs to the language, since S→‘b‘A→‘baa‘S \rightarrow `b`A \rightarrow `baa`. No other strings are generated.

Some grammars generate infinite languages, and others generate empty languages.

You are given a context-free grammar over the terminals a and b. Determine whether the language it generates contains a string with strictly more a characters than b characters.

The nonterminals are numbered from 1 to nn. The starting nonterminal is always 1.

Input

The input contains one or more test cases.

The first line of each test case contains two integers nn and mm, the number of nonterminals and the number of production rules (1≤n≤1001 \le n \le 100, 1≤m≤50 0001 \le m \le 50\,000).

Each of the next mm lines describes one rule. It starts with AiA_i and kik_i, where AiA_i (1≤Ai≤n1 \le A_i \le n) is the left side nonterminal and kik_i (0≤ki≤1000 \le k_i \le 100) is the length of the right side. Then kik_i symbols follow. Each symbol is either a nonterminal Bi,jB_{i,j} (1≤Bi,j≤n1 \le B_{i,j} \le n) or a terminal a or b. Symbols are separated by single spaces.

The sum of nn over all test cases does not exceed 1000. The sum of mm over all test cases does not exceed 50 000. The input size does not exceed 5 megabytes.

The input ends with a line containing two zeroes.

Output

For each test case, print one line. Print YES if the language contains a string with strictly more a characters than b characters. Otherwise, print NO.

Examples1

  1. Example 1

    Input
    2 2
    1 2 a 2
    2 1 b
    2 2
    1 2 b 2
    2 2 a a
    2 2
    1 2 b 2
    2 3 a a 1
    0 0
    
    Expected output
    NO
    YES
    NO