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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <list>
#include <algorithm>

using namespace std;



void run(int t) {
	//printf("====================== ITERATION %d\n", t);
	int n;
	string initial, target;
	cin >> n;
	cin >> initial >> target;
	
	vector<vector<int> > adj;
	adj.clear();
	for (int i = 0; i < n; ++i) {
		adj.push_back(vector<int>());
	}
	
	for (int i = 0; i < n-1; ++i) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		//printf("%d-ty %d and %d\n", i, a, b);
		adj[a].push_back(b);
		adj[b].push_back(a);
		//printf("Wczytano %d-ty\n", i);
	}
	//printf("Dane wczytane\n");
	
	int initial_white_exists = 0;
	int target_white_exists = 0;
	int initial_black_exists = 0;
	int target_black_exists = 0;
	for (int i = 0; i < n; ++i) {
		if (initial[i] == '0') {initial_white_exists = 1;} else {initial_black_exists = 1;};
		if (target[i] == '0') {target_white_exists = 1;} else {target_black_exists = 1;};
	}

	// WARUNEK 1
	if (initial == target) {
		cout << "TAK" << endl;
		return;
	}
	// WARUNEK 2: CZY SĄ POTRZEBNE KOLORY
	if (target_white_exists == 1 && initial_white_exists == 0) {
		cout << "NIE" << endl;
		return;
	}
	if (target_black_exists == 1 && initial_black_exists == 0) {
		cout << "NIE" << endl;
		return;
	}
	// WARUNEK 3: SPRAWDŹ CZY NIE SNEJK
	int max_stopien = 0;
	for (int i = 0; i < n; ++i) { max_stopien = adj[i].size() > max_stopien ? adj[i].size() : max_stopien; }
	if (max_stopien < 3) {
		//printf("SNEJK\n");
		vector<char> initial_groups;
		vector<char> target_groups;
		
		// znajdź startowy wierzchołek
		int startowy = 0;
		for (int i = 0; i < n; ++i) {
			if (adj[i].size() == 1) {
				startowy = i;
				break;
			}
		}
		
		int current = startowy;
		set<int> odwiedzone;
		odwiedzone.clear();
		
		while (odwiedzone.size() < n) {
			//printf("ZWIEDZAMY ŚCIEŻKĘ --> %d\n", i);
			odwiedzone.insert(current);
			if (initial_groups.size() == 0 || initial[current] != initial_groups.back()) {
				initial_groups.push_back(initial[current]);
			}
			
			if (target_groups.size() == 0 || target[current] != target_groups.back()) {
				target_groups.push_back(target[current]);
			}
			
			for (int j = 0; j < adj[current].size(); ++j) {
				if (odwiedzone.find(adj[current][j]) == odwiedzone.end()) {
					current = adj[current][j];
					break;
				}
			}
		}
		
		if (initial_groups.size() < target_groups.size()) {
			//printf("Target ma więcej grup\n");
			cout << "NIE" << endl;
			return;
		}
		if (initial_groups.size() > target_groups.size()) {
			//printf("INITIAL ma więcej grup\n");
			cout << "TAK" << endl;
			return;
		}
		
		
		int are_equal = 1;
		for (int i = 0; i < n; ++i) {
			if (initial_groups[i] != target_groups[i]) { are_equal = 0; }
		}
		if (are_equal) {
			//printf("Grupy są takie same\n");
			cout << "TAK" << endl;
			return;
		}
		else  {
			//printf("Grupy nie są takie same\n");
			cout << "NIE" << endl;
			return;
		}
	}
	// SĄ WIERZCHOŁKI STOPNIA >= 3
	else {
		//int zachodzi_dla_wszystkich = 1;
		//for (int i = 0; i < n; ++i) {
		//	char kolor_w = target[i];
		//	int wszystkie_sa_rozne = 1;
		//	for (int j = 0; j < adj[i].size(); ++j) {
		//		char kolor_somsiada = target[adj[i][j]];
		//		if (kolor_w == kolor_somsiada) wszystkie_sa_rozne = 0;
		//	}
		//	if (wszystkie_sa_rozne == 0) zachodzi_dla_wszystkich = 0;
		//}
		//	
		//if (zachodzi_dla_wszystkich == 1) {
		//	cout << "NIE" << endl;
		//	return;
		//} else {
		//	cout << "TAK" << endl;
		//	return;
		//}
		// sprawdź czy istnieją dwa sąsiednie w tego samego koloru
		int istnieje = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < adj[i].size(); ++j) {
				if (target[i] == target[adj[i][j]]) {
					istnieje = 1;
				}
			}
		}
		if (istnieje == 1) {
			cout << "TAK" << endl;
			return;
		} else {
			cout << "NIE" << endl;
			return;
		}
	}
	cout << "NIE" << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) run(i);
}