Given the blood groups of N parents under N-allele inheritance, decide for each of Q queries whether the asked blood group can appear in their child.
Medium7GraphCombinatoricsNo attempts yetTime limit3sMemory limit256 MBHumans have four blood groups: AB, A, B and O. They mean that the red blood cells carry antigens of both type A and type B, only type A, only type B, or no antigen at all. Two alleles in our DNA determine the blood group, and the type of each allele is A, B or O. The table below lists the allele combinations someone may have for each blood group.
| Blood group | AB | A | B | O |
|---|---|---|---|---|
| Possible alleles | AB | OA, AA | OB, BB | OO |
We inherit exactly one allele from each of our two parents. So, given the blood groups of the two parents, we can say for sure whether a blood group is possible in their offspring. For example, if the blood groups of the two parents are AB and B, their possible allele combinations are {AB} and {OB, BB}. The order of the alleles does not matter, so the possible allele combinations for the offspring are {OA, AB, OB, BB}. That means the blood groups AB, A and B are possible in the offspring, and the blood group O is not.
Now suppose life on Earth had evolved so that an individual has three parents, three alleles and three antigen types. The allele combinations would look like this.
| Blood group | ABC | AB | AC | BC | A | B | C | O |
|---|---|---|---|---|---|---|---|---|
| Possible alleles | ABC | OAB, AAB, ABB | OAC, AAC, ACC | OBC, BBC, BCC | OOA, OAA, AAA | OOB, OBB, BBB | OOC, OCC, CCC | OOO |
If the blood groups of the three parents are A, BC and O, then every blood group except BC and ABC is possible in their offspring.
The universe is vast. Somewhere out in space there may be a form of life whose individuals have N parents, N alleles and N antigen types. You are given the blood groups of the N parents and a list of Q blood groups to test. Decide which of them are possible in the offspring of those parents and which are not.
The first line contains two integers N and Q. N is the number of parents, which is also the number of alleles and the number of antigen types, and Q is the number of queries (1≤N≤100, 1≤Q≤40).
Each of the next N lines describes the blood group of one parent. Each of the following Q lines describes a blood group to test.
Antigen types are identified with distinct integers from 1 to N instead of letters. Each line describing a blood group contains an integer B, the number of antigen types in that blood group (0≤B≤N), followed by B different integers C1,C2,…,CB representing the antigen types present in it (1≤Ci≤N).
For each of the Q queries, print one line with the uppercase letter "Y" if the corresponding blood group is possible in the offspring of the given parents, and the uppercase letter "N" otherwise. Print the results in the same order that the queries appear in the input.