Grammar

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

요약
터미널이 a와 b뿐인 문맥 자유 문법이 주어질 때, 생성되는 언어에 a가 b보다 많은 문자열이 있는지 판정한다.
난이도

어려움10점 중 8점

유형
그래프, 동적 계획법, 그리디, DFS
정답자
아직 제출이 없습니다

문제

A formal grammar is a way of describing formal languages 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 called an аlphabet and its elements are called terminals, NN is a set of nonterminals, SS is the starting nonterminal, and PP is a set of production rules of the form α→β\alpha \rightarrow \beta.

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

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

For example, let us consider a grammar from the second example test case with alphabet Σ=‘a‘,‘b‘\Sigma = \\{`a`, `b`\\}, set of nonterminals N=S,AN = \\{S, A\\} and two production rules:

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

One can easily see that it is a context-free grammar.

To create the language generated by a grammar, one needs to start from a string consisting of only start nonterminal SS, and then apply production rules one or more times. Applying a production rule is the procedure of finding the left side of that rule somewhere in the current string and replacing it by the string from the right side of that rule. The language generated by Γ\Gamma is the set of all strings consisting only of terminals that can be produced by applying production rules one or more times.

For example, there is a string ‘baa‘`baa` in the language generated by the grammar described above. To produce it, one could apply productions S→‘b‘A→‘baa‘S \rightarrow `b`A \rightarrow `baa`. There are no other strings in the language generated by this grammar.

Some grammars may even generate infinite languages, others may generate empty ones.

You are given a context-free grammar with an alphabet consisting of two terminals "a" and "b". Your task is to check whether the language generated by this grammar contains a string consisting of strictly more characters "a" than characters "b".

Nonterminals in this task are enumerated from 11 to nn. The starting nonterminal always has number 11.

입력

The input consists of 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 production rule in the following manner. At first, A_iA\_i and k_ik\_i are given: the number of left side nonterminal (1≤A_i≤n1 \le A\_i \le n) and the number of characters on the right side of the production rule (0≤k_i≤1000 \le k\_i \le 100). Then k_ik\_i objects follow, each of them is either a nonterminal B_i,jB\_{i,j} (1≤B_i,j≤n1 \le B\_{i,j} \le n) or a terminal "a" or "b". Consecutive characters are separated by single spaces.

The total sum of all nn over all test cases does not exceed 10001000. The total sum of all mm over all test cases does not exceed 50,00050\\,000. The size of the input does not exceed 55 megabytes.

The input is terminated by a string of two zeroes.

출력

For each test case, output a separate line. It must contain "YES" if the language generated by the given grammar contains a string consisting of strictly more characters "a" than characters "b", otherwise the line must contain "NO".

예제1

  1. 예제 1

    입력
    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
    
    예상 출력
    NO
    YES
    NO