// #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <algorithm> using namespace std; void solve() { int n; cin >> n; string g1, g2; cin >> g1 >> g2; int v1, v2; unordered_map<int, unordered_map<int, bool>> vxs; bool g2HasNeighbooringSame = false; for (int i = 0; i < n - 1; i++) { cin >> v1 >> v2; v1--; v2--; vxs[v1][v2] = true; vxs[v2][v1] = true; if (g2[v1] == g2[v2]) g2HasNeighbooringSame = true; } int r1 = 0; int b1 = 0; int r2 = 0; int b2 = 0; vector<int> diff; for (int i = 0; i < n; i++) { if (g1[i] == '1') r1++; else b1++; if (g2[i] == '1') r2++; else b2++; if (g1[i] != g2[i]) diff.push_back(i); } if ((!r1 && r2) || (!b1 && b2)) { cout << "NIE" << endl; return; } if ((r1 && !r2) || (b1 && !b2) || diff.size() == 0) { cout << "TAK" << endl; return; } if (r1 && b1 && g2HasNeighbooringSame) { // checking for triplets int leafIdx; for (int i = 0; i < n; i++) { if (vxs[i].size() >= 3) { cout << "TAK" << endl; return; } if (vxs[i].size() == 1) { leafIdx = i; } } // how many changes across lane XD int previousIdx = leafIdx; int currIdx = leafIdx; int ch1 = 0; int ch2 = 0; for (int i = 0; i < n - 1; i++) { for (auto nbr = vxs[currIdx].begin(); nbr != vxs[currIdx].end(); nbr++) { if ((*nbr).first != previousIdx) { previousIdx = currIdx; currIdx = (*nbr).first; break; } } if (g1[currIdx] != g1[previousIdx]) ch1++; if (g2[currIdx] != g2[previousIdx]) ch2++; } if ((ch1 == ch2 + 1 && (g1[currIdx] == g2[currIdx] || g1[leafIdx] == g2[leafIdx])) || (ch1 == ch2 && (g1[currIdx] == g2[currIdx] && g1[leafIdx] == g2[leafIdx])) || (ch1 > ch2 + 1)) { cout << "TAK" << endl; return; } } cout << "NIE" << endl; return; } int main() { FAST int n; cin >> n; for (int i = 0; i < n; i++) { solve(); } }
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 | // #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <unordered_map> #include <queue> #include <algorithm> using namespace std; void solve() { int n; cin >> n; string g1, g2; cin >> g1 >> g2; int v1, v2; unordered_map<int, unordered_map<int, bool>> vxs; bool g2HasNeighbooringSame = false; for (int i = 0; i < n - 1; i++) { cin >> v1 >> v2; v1--; v2--; vxs[v1][v2] = true; vxs[v2][v1] = true; if (g2[v1] == g2[v2]) g2HasNeighbooringSame = true; } int r1 = 0; int b1 = 0; int r2 = 0; int b2 = 0; vector<int> diff; for (int i = 0; i < n; i++) { if (g1[i] == '1') r1++; else b1++; if (g2[i] == '1') r2++; else b2++; if (g1[i] != g2[i]) diff.push_back(i); } if ((!r1 && r2) || (!b1 && b2)) { cout << "NIE" << endl; return; } if ((r1 && !r2) || (b1 && !b2) || diff.size() == 0) { cout << "TAK" << endl; return; } if (r1 && b1 && g2HasNeighbooringSame) { // checking for triplets int leafIdx; for (int i = 0; i < n; i++) { if (vxs[i].size() >= 3) { cout << "TAK" << endl; return; } if (vxs[i].size() == 1) { leafIdx = i; } } // how many changes across lane XD int previousIdx = leafIdx; int currIdx = leafIdx; int ch1 = 0; int ch2 = 0; for (int i = 0; i < n - 1; i++) { for (auto nbr = vxs[currIdx].begin(); nbr != vxs[currIdx].end(); nbr++) { if ((*nbr).first != previousIdx) { previousIdx = currIdx; currIdx = (*nbr).first; break; } } if (g1[currIdx] != g1[previousIdx]) ch1++; if (g2[currIdx] != g2[previousIdx]) ch2++; } if ((ch1 == ch2 + 1 && (g1[currIdx] == g2[currIdx] || g1[leafIdx] == g2[leafIdx])) || (ch1 == ch2 && (g1[currIdx] == g2[currIdx] && g1[leafIdx] == g2[leafIdx])) || (ch1 > ch2 + 1)) { cout << "TAK" << endl; return; } } cout << "NIE" << endl; return; } int main() { FAST int n; cin >> n; for (int i = 0; i < n; i++) { solve(); } } |