The Melancholy of Problem Setter CodeChef Solution

Problem : The Melancholy of Problem Setter Code Chef Solution

Roughly speaking, this problem asks you to create the checker of the problem used in December CookOff.

You are given a tree with NN vertices and a string CC of length NN, consisting of the characters 'R''G' and 'B'.

For each integer i (1≤i≤N)i (1≤i≤N), CiCi is the color of vertex ii — 'R' is for Red, 'G' is for Green and 'B' is for Blue.

The input is called valid if for all pairs of Blue vertices (u, v), u≠v(u, v), u≠v, there is at least one Red vertex and one Green vertex on the simple path between uu and vv.

Your task is to validate the input for given TT test cases. For each test case, print Yes if the input is valid, otherwise No.

Input Format

  • The first line of input contains one positive integer TT, the number of test cases. The description of TT test cases follows.
  • The first line of each test case contains one positive integer NN, the number of vertices of the tree.
  • The second line of each test case contains a string CC, denoting the colors of the vertices.
  • Each of the following N−1N−1 lines contains two space-separated integers uu and vv, denoting an edge between vertices uu and vv in the tree.

Output Format

For each test case, output a single line containing Yes if the input is valid, and No otherwise.

You may print each character of the string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).

Constraints

  • 1≤T≤1041≤T≤104
  • 1≤N≤2⋅1051≤N≤2⋅105
  • The sum of NN for each test case is at most 2⋅1052⋅105
  • |C|=N|C|=N
  • CC consists of three types of characters, 'R''G' and 'B'
  • 1≤u,v≤N,u≠v1≤u,v≤N,u≠v
  • The input graph forms a tree

Sample Input 1 

5
4
BRGB
3 2
1 2
4 3
3
RGB
1 2
2 3
1
B
2
BB
1 2
4
BRGB
3 2
1 2
4 2

Sample Output 1 

Yes
Yes
Yes
No
No

Explanation

  • Test case 1:
    • The only pair is (1,4)(1,4).
  • Test case 3:
    • There are no pairs.
  • Test case 5:
    • The only pair is (1,4)(1,4). There are no Green vertices on the path between 11 and 44.

Get More CodeChef Solution >>

Substring Minimum Function FizzBuzz Solution

Magical Planks Fizzbuzz Solution

Leave a Reply

error: Content is protected !!