1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <algorithm>
#include <stdio.h>
#include <vector>

bool isColorsSame(const std::vector<bool> &inputColor, const std::vector<bool> &outputColor, const int n)
{
	for (int i = 1; i <= n; ++i)
		if (inputColor[i] != outputColor[i])
			return false;
	return true;
}

int countColors(const std::vector<bool> &color, const int n)
{
	bool zeroColor{false}, oneColor{false};
	for (int i = 1; i <= n; ++i)
	{
		zeroColor = zeroColor || !color[i];
		oneColor = oneColor || color[i];
	}
	return zeroColor + oneColor;
}

int countMixedEdge(const std::vector<bool> &color, const std::vector<std::pair<int, int>> &edges)
{
	int sum{0};
	for (const auto &[a, b] : edges)
		if (color[a] != color[b])
			sum++;
	return sum;
}

bool solve(
		const std::vector<bool> &inputColor,
		const std::vector<bool> &outputColor,
		const std::vector<std::pair<int, int>> &edges,
		const std::vector<int> &count,
		const int n)
{
	if (isColorsSame(inputColor, outputColor, n))
		return true;

	{
		const auto inputColors = countColors(inputColor, n);
		const auto outputColors = countColors(outputColor, n);
		if (inputColors < outputColors || (inputColors == 1 && outputColors == 1))
			return false;
	}

	if (2 < *std::max_element(count.cbegin(), count.cend()))
	{
		for (const auto &[a, b] : edges)
			if (outputColor[a] == outputColor[b])
				return true;
		return false;
	}
	else
	{
		const int inputMixedEdge = countMixedEdge(inputColor, edges);
		const int outputMixedEdge = countMixedEdge(outputColor, edges);
		if (inputMixedEdge == outputMixedEdge)
		{
			int lowest{1};
			for (int i = 1; i <= n; ++i)
				if (count[i] == 1)
				{
					lowest = i;
					break;
				}
			return inputColor[lowest] == outputColor[lowest];
		}
		else
		{
			return outputMixedEdge <= inputMixedEdge;
		}
	}
}

int main()
{
	int t, n, a, b;
	char color[100001];
	scanf("%d", &t);
	for (int i = 1; i <= t; ++i)
	{
		scanf("%d", &n);
		std::vector<bool> inputColor;
		std::vector<bool> outputColor;
		std::vector<int> count;
		std::vector<std::pair<int, int>> edges;
		inputColor.resize(n + 1);
		outputColor.resize(n + 1);
		count.resize(n + 1);

		scanf("%s", color);
		for (int i = 0; i < n; ++i)
			inputColor[i + 1] = color[i] == '1';

		scanf("%s", color);
		for (int i = 0; i < n; ++i)
			outputColor[i + 1] = color[i] == '1';

		for (int i = 1; i <= n - 1; ++i)
		{
			scanf("%d %d", &a, &b);
			edges.push_back({a, b});
			count[a]++;
			count[b]++;
		}
		if (solve(inputColor, outputColor, edges, count, n))
			printf("TAK\n");
		else
			printf("NIE\n");
	}
	return 0;
}