#P9039. [PA 2021] Drzewo czerwono-czarne
[PA 2021] Drzewo czerwono-czarne
Problem Description
Are you familiar with the data structure called a red-black tree? In this problem, we will consider a tree whose nodes are colored red or black, but rest assured: if you have heard of the data structure mentioned above, it is best to forget it quickly.
You are given a tree (that is, a connected undirected graph with no cycles), where each node is colored either red or black. You may choose two adjacent nodes and (connected by an edge), and recolor to have the same color as .
Your task is to determine whether, after a sequence of operations (possibly performing no operations at all), an initial coloring can be transformed into the given final coloring.
Input Format
This problem has multiple test cases.
The first line contains an integer , the number of test cases.
For each test case:
The first line contains an integer , the number of nodes in the tree.
The second line contains characters, each being 0 or 1. If the -th character is 0, then initially the -th node is colored red. If the -th character is 1, then initially the -th node is colored black.
The third line contains characters, each being 0 or 1. If the -th character is 0, then in the end the -th node must be colored red. If the -th character is 1, then in the end the -th node must be colored black.
The next lines each contain two integers , describing an edge in the tree.
Output Format
For each test case:
Output one line with a string. If there exists a sequence of operations that transforms the coloring into the given final coloring, output TAK; otherwise output NIE.
3
4
1011
1100
1 2
2 3
2 4
2
10
10
1 2
2
10
01
1 2
TAK
TAK
NIE
Hint
Constraints: For of the testdata, , , .
Translated by ChatGPT 5