#include <bits/stdc++.h>
using namespace std;
void test_case()
{
int n;
string s1, s2;
cin >> n >> s1 >> s2;
s1 = "#" + s1;
s2 = "#" + s2;
int ilezer1 = 0, ilejed1 = 0, ilezer2 = 0, ilejed2 = 0;
vector<vector<int>> g(n + 1);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
int maxdeg = 0;
for (int i = 1; i <= n; i++)
maxdeg = max(maxdeg, (int)size(g[i]));
if (s1 == s2) {
cout << "TAK\n";
return;
}
for (int i = 1; i <= n; i++) {
ilezer1 += (s1[i] == '0');
ilezer2 += (s2[i] == '0');
ilejed1 += (s1[i] == '1');
ilejed2 += (s2[i] == '1');
}
if ((ilezer2 && !ilezer1) || (ilejed2 && !ilejed1)) {
cout << "NIE\n";
return;
}
if (ilezer2 == n || ilejed2 == n) {
cout << "TAK\n";
return;
}
if (n == 1 || maxdeg == 1) {
cout << (s1 == s2 ? "TAK\n" : "NIE\n");
return;
}
if (maxdeg == 2) {
string sn1 = "", sn2 = "";
function<void(int, int, char)> dfs = [&](int u, int p, char l) {
for (auto to : g[u]) {
if (to == p) continue;
if (s1[to] != l) sn1.push_back(s1[to]);
dfs(to, u, s1[to]);
}
};
for (int i = 1; i <= n; i++) {
if (g[i].size() == 1) {
sn1.push_back(s1[i]), sn2.push_back(s2[i]);
for (int rep : {0, 1}) {
dfs(i, i, s1[i]);
swap(s1, s2);
swap(sn1, sn2);
}
break;
}
}
if (size(sn1) > size(sn2))
cout << "TAK\n";
else if (size(sn1) < size(sn2) || sn1[0] != sn2[0])
cout << "NIE\n";
else
cout << "TAK\n";
return;
}
for (int i = 1; i <= n; i++) {
for (auto to : g[i]) {
if (s2[i] == s2[to]) {
cout << "TAK\n";
return;
}
}
}
cout << "NIE\n";
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while (t--)
test_case();
}
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 | #include <bits/stdc++.h> using namespace std; void test_case() { int n; string s1, s2; cin >> n >> s1 >> s2; s1 = "#" + s1; s2 = "#" + s2; int ilezer1 = 0, ilejed1 = 0, ilezer2 = 0, ilejed2 = 0; vector<vector<int>> g(n + 1); for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].emplace_back(b); g[b].emplace_back(a); } int maxdeg = 0; for (int i = 1; i <= n; i++) maxdeg = max(maxdeg, (int)size(g[i])); if (s1 == s2) { cout << "TAK\n"; return; } for (int i = 1; i <= n; i++) { ilezer1 += (s1[i] == '0'); ilezer2 += (s2[i] == '0'); ilejed1 += (s1[i] == '1'); ilejed2 += (s2[i] == '1'); } if ((ilezer2 && !ilezer1) || (ilejed2 && !ilejed1)) { cout << "NIE\n"; return; } if (ilezer2 == n || ilejed2 == n) { cout << "TAK\n"; return; } if (n == 1 || maxdeg == 1) { cout << (s1 == s2 ? "TAK\n" : "NIE\n"); return; } if (maxdeg == 2) { string sn1 = "", sn2 = ""; function<void(int, int, char)> dfs = [&](int u, int p, char l) { for (auto to : g[u]) { if (to == p) continue; if (s1[to] != l) sn1.push_back(s1[to]); dfs(to, u, s1[to]); } }; for (int i = 1; i <= n; i++) { if (g[i].size() == 1) { sn1.push_back(s1[i]), sn2.push_back(s2[i]); for (int rep : {0, 1}) { dfs(i, i, s1[i]); swap(s1, s2); swap(sn1, sn2); } break; } } if (size(sn1) > size(sn2)) cout << "TAK\n"; else if (size(sn1) < size(sn2) || sn1[0] != sn2[0]) cout << "NIE\n"; else cout << "TAK\n"; return; } for (int i = 1; i <= n; i++) { for (auto to : g[i]) { if (s2[i] == s2[to]) { cout << "TAK\n"; return; } } } cout << "NIE\n"; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int t; cin >> t; while (t--) test_case(); } |
English