Grammar
시간 제한2초메모리 제한1024 MB
터미널이 a와 b뿐인 문맥 자유 문법이 주어질 때, 생성되는 언어에 a가 b보다 많은 문자열이 있는지 판정한다.
문제
A formal grammar is a way of describing formal languages as where is called an аlphabet and its elements are called terminals, is a set of nonterminals, is the starting nonterminal, and is a set of production rules of the form .
Here, contains all strings of one or more elements of (non-empty strings of nonterminals), and consists of all strings of zero, one or more elements of (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, .
For example, let us consider a grammar from the second example test case with alphabet , set of nonterminals and two production rules:
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 , 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 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 in the language generated by the grammar described above. To produce it, one could apply productions . 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 to . The starting nonterminal always has number .
입력
The input consists of one or more test cases.
The first line of each test case contains two integers and : the number of nonterminals and the number of production rules (, ).
Each of the next lines describes one production rule in the following manner. At first, and are given: the number of left side nonterminal () and the number of characters on the right side of the production rule (). Then objects follow, each of them is either a nonterminal () or a terminal "a" or "b". Consecutive characters are separated by single spaces.
The total sum of all over all test cases does not exceed . The total sum of all over all test cases does not exceed . The size of the input does not exceed 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".