Under construction

Still being built. Things may change

Hat Puzzle

Time limit10sMemory limit1024 MB

Summary
Given who can see whose hat, simulate rounds of public yes/no answers over all hat configurations and output each asked setter's answer.
Level

Medium6 of 10

Topics
Simulation, Bit manipulation, Brute force
Solved
No attempts yet

Problem

NN UCPC problem setters have gathered in one place to record a solution explanation and a scoreboard broadcast. Before the broadcast starts, the setters play a mind game for fun.

  • The game host has many white and black hats.
  • Each setter sits in an assigned chair. Every setter knows who can see whose hat, and no one can see their own hat.
  • When every setter closes their eyes, the host puts one of the two colored hats on each setter. The host then says, "The number of white hats is at least w1w_1 and at most w2w_2."
  • After that, the host asks several questions. In each question, the host asks some setters at the same time, "Do you know the color of the hat you are wearing?" All setters asked must answer at the same time. Every setter can hear what each setter answered.

Solve this game from the viewpoint of an observer who knows the hat color of every setter. Assume every setter reasons perfectly logically and makes no guesses without evidence. Find what each setter answers.

Input

The first line contains NN (1≤N≤201 \leq N \leq 20).

Each of the next NN lines contains NN values, each either 0 or 1. The jj-th value on the ii-th line is 1 if setter ii can see the hat of setter jj, and 0 otherwise. No setter can see their own hat, so the diagonal values are 0.

The next line contains w1w_1 and w2w_2, the minimum and maximum number of white hats announced by the host (0≤w1≤w2≤N0 \leq w_1 \leq w_2 \leq N).

The next line contains a string of length NN made of W and B. The ii-th character is W if setter ii wears a white hat and B otherwise. The number of W characters is between w1w_1 and w2w_2, inclusive.

The next line contains the number of questions QQ (1≤Q≤301 \leq Q \leq 30).

Each of the next QQ lines starts with kk, the number of setters asked in that question, followed by kk distinct integers xx, each naming a setter who is asked. All kk setters are asked at the same time.

Output

For each question, print one line with kk characters. Each character is Y if the setter answers yes, or N if the setter answers no, in the order the setters were listed in the question.

Examples2

  1. Example 1

    Input
    3
    0 1 1
    0 0 1
    0 0 0
    1 3
    BWW
    3
    1 1
    1 2
    1 3
    
    Expected output
    N
    N
    Y
    
  2. Example 2

    Input
    6
    0 1 1 1 1 1
    1 0 1 1 1 1
    1 1 0 1 1 1
    1 1 1 0 1 1
    1 1 1 1 0 1
    1 1 1 1 1 0
    1 6
    WWWBBB
    4
    6 1 2 3 4 5 6
    6 1 2 3 4 5 6
    6 1 2 3 4 5 6
    6 1 2 3 4 5 6
    
    Expected output
    NNNNNN
    NNNNNN
    YYYNNN
    YYYYYY