QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 512 MB 満点: 10 難易度: [表示]

#2128. Red-black tree [C]

統計

Are you familiar with the data structure known as a red-black tree? In this problem, we will consider trees with red or black vertices, but don't worry—if you have heard of the aforementioned structure, it is best to quickly forget about it.

You are given a tree (a connected undirected graph without cycles) in which each vertex is painted one of two colors: red or black. The operation you can perform is to choose two vertices $v$ and $u$ connected by an edge and repaint $v$ with the color that $u$ is painted with.

problem_2128_1.png

Your task is to determine whether it is possible to obtain a given final configuration of colors from an initial configuration after some (possibly empty) sequence of operations.

Input

The first line of the input contains a single integer $t$ ($1 \le t \le 10^5$), representing the number of test cases.

The descriptions of the test cases follow. Each test case description begins with a line containing a single integer $n$ ($1 \le n \le 10^5$), representing the number of vertices in the tree.

The next line contains a string consisting of $n$ characters, each of which is 0 or 1. If the $i$-th character is 0, then the $i$-th vertex is initially painted red. If the $i$-th character is 1, then the $i$-th vertex is initially painted black.

The next line contains a string consisting of $n$ characters, each of which is 0 or 1, which describes in the same way whether each vertex should be red or black after performing the operations, where 0 also denotes red and 1 denotes black.

The next $n-1$ lines each contain two integers. The $j$-th of these lines contains integers $a_j$ and $b_j$ ($1 \le a_j, b_j \le n; a_j \neq b_j$), indicating that vertices $a_j$ and $b_j$ are connected by an edge. You may assume that the given sequence of edges describes a valid tree.

The sum of $n$ over all test cases does not exceed $10^6$.

Output

The output should contain $t$ lines. If in the $k$-th test case it is possible to bring the tree to the desired state, the $k$-th line should contain the single word TAK. Otherwise, it should contain the single word NIE.

Examples

Input 1

3
4
1011
1100
1 2
2 3
2 4
2
10
10
1 2
2
10
01
1 2

Output 1

TAK
TAK
NIE

Note

Explanation of the example: In the first test case, we can first repaint the third vertex with the color of the second vertex, and then repaint the fourth vertex with the color of the second vertex. In this way, the last remaining vertex of the black color is the first vertex. It is therefore sufficient to now repaint the second vertex with the color of the first vertex. After these three operations, all vertex colors match the given final configuration.

problem_2128_2.png

In the second test case, we do not need to perform any operations – both vertices already have the correct color initially.

In the third test case, it is not possible to swap the colors of the vertices.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.