#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int z;
cin >> z;
while (z--) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
vector<vector<int>> graph(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
graph[a].push_back(b);
graph[b].push_back(a);
}
if (n==1) {
cout << (s[0] == t[0] ? "TAK" : "NIE");
continue;
}
bool is_path = true;
for (int i = 0; i < n; ++i) {
if (graph[i].size() >= 3)
is_path = false;
}
if (is_path) {
int leaf = -1;
for (int i = 0; i < n; ++i)
if (graph[i].size() == 1)
leaf = i;
assert(leaf != -1);
int changes_s = 0, changes_t = 0;
int cur = leaf, prv = -1;
do {
int nxt = graph[cur][0];
if (nxt == prv)
nxt = graph[cur][1];
if (s[cur] != s[nxt])
changes_s++;
if (t[cur] != t[nxt])
changes_t++;
prv = cur;
cur = nxt;
} while (graph[cur].size() != 1);
if (s[leaf] != t[leaf])
changes_s--;
if (changes_s % 2 != changes_t % 2)
changes_s--;
cout << (changes_s >= changes_t ? "TAK" : "NIE") << endl;
} else {
// 1) s is mono, t contains other color -> NO
bool is_s_monocolor = true;
for (int i = 0; i < n - 1; ++i)
if (s[i] != s[i + 1])
is_s_monocolor = false;
if (is_s_monocolor) {
bool does_t_contain_other_color = false;
for (char c : t)
if (c != s[0])
does_t_contain_other_color = true;
if (does_t_contain_other_color) {
cout << "NIE" << endl;
continue;
}
}
// 2) t is proper colors, s is different -> NO
bool is_t_proper = true;
for (int u = 0; u < n; ++u)
for (int v : graph[u])
if (t[u] == t[v])
is_t_proper = false;
if (is_t_proper && s != t) {
cout << "NIE" << endl;
continue;
}
cout << "TAK" << endl;
}
}
}
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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int z; cin >> z; while (z--) { int n; cin >> n; string s, t; cin >> s >> t; vector<vector<int>> graph(n); for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; --a; --b; graph[a].push_back(b); graph[b].push_back(a); } if (n==1) { cout << (s[0] == t[0] ? "TAK" : "NIE"); continue; } bool is_path = true; for (int i = 0; i < n; ++i) { if (graph[i].size() >= 3) is_path = false; } if (is_path) { int leaf = -1; for (int i = 0; i < n; ++i) if (graph[i].size() == 1) leaf = i; assert(leaf != -1); int changes_s = 0, changes_t = 0; int cur = leaf, prv = -1; do { int nxt = graph[cur][0]; if (nxt == prv) nxt = graph[cur][1]; if (s[cur] != s[nxt]) changes_s++; if (t[cur] != t[nxt]) changes_t++; prv = cur; cur = nxt; } while (graph[cur].size() != 1); if (s[leaf] != t[leaf]) changes_s--; if (changes_s % 2 != changes_t % 2) changes_s--; cout << (changes_s >= changes_t ? "TAK" : "NIE") << endl; } else { // 1) s is mono, t contains other color -> NO bool is_s_monocolor = true; for (int i = 0; i < n - 1; ++i) if (s[i] != s[i + 1]) is_s_monocolor = false; if (is_s_monocolor) { bool does_t_contain_other_color = false; for (char c : t) if (c != s[0]) does_t_contain_other_color = true; if (does_t_contain_other_color) { cout << "NIE" << endl; continue; } } // 2) t is proper colors, s is different -> NO bool is_t_proper = true; for (int u = 0; u < n; ++u) for (int v : graph[u]) if (t[u] == t[v]) is_t_proper = false; if (is_t_proper && s != t) { cout << "NIE" << endl; continue; } cout << "TAK" << endl; } } } |
English