Under construction

Still being built. Things may change

Palindromic DNA

Time limit3sMemory limit128 MB

Summary
Decide, given a cyclic DNA alphabet and palindrome constraints on many index subsets with a no-adjacent-change rule, whether a valid ±1/0 edit assignment exists.
Level

Hard8 of 10

Topics
Union-find, Graph, Implementation, Math
Solved
No attempts yet

Problem

A DNA sequence is a string over four nucleobases — Adenine, Guanine, Thymine, and Cytosine — which we write using their initials A, G, T, and C. The bases have a cyclic order: A is followed by G, G by T, T by C, and C back to A.

Recent genomics research suggests that some diseases are linked to certain groups of bases failing to read as a palindrome. You are given a DNA string SS (indexed from 00) together with tt subsets of positions P1,…,PtP_1, \dots, P_t, and you must transform SS so that, for every subset, the characters of SS at those positions read the same forwards and backwards. Formally, the restriction of SS to a subset P={i1,i2,…,ik}P = \{i_1, i_2, \dots, i_k\} with 0≤i1<i2<⋯<ik<∣S∣0 \le i_1 < i_2 < \dots < i_k < |S| is the string Si1Si2⋯SikS_{i_1} S_{i_2} \cdots S_{i_k}, and every such restriction must be a palindrome.

You may inspect any base, but each base can be changed in only one of three ways:

  1. Leave it unchanged.
  2. Advance it by one step in the cyclic order (for example, C becomes A).
  3. Move it back by one step (for example, T becomes G).

Because of a technological limitation, you may not change two bases that sit in consecutive positions of the sequence. Decide whether the goal is achievable.

For example, take the sequence AGTAT with subsets P1={1,4}P_1 = \{1, 4\}, P2={0,1}P_2 = \{0, 1\}, and P3={0,2,4}P_3 = \{0, 2, 4\}. Advancing the first base and moving the last base back gives GGTAG; the three restrictions become GG, GG, and GTG, all palindromes, and the two changed positions (00 and 44) are not consecutive.

By contrast, the sequence CATGC with the two subsets {0,3}\{0, 3\} and {3,4}\{3, 4\} has no solution: positions 00, 33, and 44 would all have to change to the same base (for example T), which forces the consecutive positions 33 and 44 to be modified together — and that is forbidden.

Input

The input contains several test cases. The first line of a test case has two integers NN and TT (1≤N≤100001 \le N \le 10000, 1≤T≤60001 \le T \le 6000): the length of the sequence and the number of subsets. The next line contains the DNA sequence, a string of length NN over the alphabet ACGT.

Each of the following TT lines describes one subset. A line begins with L:, where LL (0≤L≤N0 \le L \le N) is the number of positions in the subset, followed by the LL distinct positions in strictly increasing order, each between 00 and N−1N - 1. Subsets may overlap partially or completely.

A blank line separates consecutive test cases. The input ends with a line containing 0 0, which must not be processed.

Output

For each test case, print a single line containing YES if all the required restrictions can be made palindromic under the rules, or NO otherwise.

Examples3

  1. Example 1

    Input
    5 3
    AGTAT
    2: 1 4
    2: 0 1
    3: 0 2 4
    
    5 3
    CATGC
    0:
    2: 0 3
    2: 3 4
    
    0 0
    
    Expected output
    YES
    NO
    
  2. Example 2

    Input
    1 1
    A
    1: 0
    0 0
    
    Expected output
    YES
    
  3. Example 3

    Input
    3 2
    AGA
    3: 0 1 2
    0:
    0 0
    
    Expected output
    YES