#include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define vll vector<ll> #define vvll vector<vll> #define vvvll vector<vvll> #define vvvvll vector<vvvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; int uses_only(str & s) { bool uses0 = false; bool uses1 = false; for (char x : s) { if (x == '1') uses1 = true; if (x == '0') uses0 = true; } if (uses0 && uses1) return -1; if (uses0) return 0; return 1; } bool exist_neighbours_of_same_color(vvi & tree, str & coloring, int n) { rep(x, 0, n) { for (int y : tree[x]) { if (coloring[y] == coloring[x]) return true; } } return false; } void path_way(vvi & tree, int x, int p, vi & path) { path.push_back(x); for (int y : tree[x]) { if (y != p) path_way(tree, y, x, path); } } void solve() { int n; cin >> n; str initial; str wanted; cin >> initial; cin >> wanted; vvi tree(n); vi deg(n, 0); int maxdeg = 0; rep(i, 0, n - 1) { int x, y; cin >> x >> y; x--; y--; tree[x].push_back(y); tree[y].push_back(x); deg[x]++; deg[y]++; maxdeg = max(maxdeg, deg[x]); maxdeg = max(maxdeg, deg[y]); } if (initial == wanted) { cout << "TAK\n"; return; } int used = uses_only(initial); if (used != -1) { cout << "NIE\n"; return; } if (!exist_neighbours_of_same_color(tree, wanted, n)) { cout << "NIE\n"; return; } if (maxdeg >= 3) { cout << "TAK\n"; return; } // ścieżka vi path; int src = 0; rep(i, 0, n) { if (deg[i] == 1) { src = i; break; } } path_way(tree, src, src, path); str reduced_initial; str reduced_wanted; int i = 0; while (i < n) { int j = i; while (j < n && initial[path[j]] == initial[path[i]]) j++; reduced_initial.push_back(initial[path[i]]); i = j; } i = 0; while (i < n) { int j = i; while (j < n && wanted[path[j]] == wanted[path[i]]) j++; reduced_wanted.push_back(wanted[path[i]]); i = j; } if (reduced_initial.size() > reduced_wanted.size()) { cout << "TAK\n"; return; } if (reduced_initial == reduced_wanted) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool _multipleTestCases = true; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { solve(); } return 0; }
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 176 177 178 179 180 181 182 183 184 | #include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define vll vector<ll> #define vvll vector<vll> #define vvvll vector<vvll> #define vvvvll vector<vvvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; int uses_only(str & s) { bool uses0 = false; bool uses1 = false; for (char x : s) { if (x == '1') uses1 = true; if (x == '0') uses0 = true; } if (uses0 && uses1) return -1; if (uses0) return 0; return 1; } bool exist_neighbours_of_same_color(vvi & tree, str & coloring, int n) { rep(x, 0, n) { for (int y : tree[x]) { if (coloring[y] == coloring[x]) return true; } } return false; } void path_way(vvi & tree, int x, int p, vi & path) { path.push_back(x); for (int y : tree[x]) { if (y != p) path_way(tree, y, x, path); } } void solve() { int n; cin >> n; str initial; str wanted; cin >> initial; cin >> wanted; vvi tree(n); vi deg(n, 0); int maxdeg = 0; rep(i, 0, n - 1) { int x, y; cin >> x >> y; x--; y--; tree[x].push_back(y); tree[y].push_back(x); deg[x]++; deg[y]++; maxdeg = max(maxdeg, deg[x]); maxdeg = max(maxdeg, deg[y]); } if (initial == wanted) { cout << "TAK\n"; return; } int used = uses_only(initial); if (used != -1) { cout << "NIE\n"; return; } if (!exist_neighbours_of_same_color(tree, wanted, n)) { cout << "NIE\n"; return; } if (maxdeg >= 3) { cout << "TAK\n"; return; } // ścieżka vi path; int src = 0; rep(i, 0, n) { if (deg[i] == 1) { src = i; break; } } path_way(tree, src, src, path); str reduced_initial; str reduced_wanted; int i = 0; while (i < n) { int j = i; while (j < n && initial[path[j]] == initial[path[i]]) j++; reduced_initial.push_back(initial[path[i]]); i = j; } i = 0; while (i < n) { int j = i; while (j < n && wanted[path[j]] == wanted[path[i]]) j++; reduced_wanted.push_back(wanted[path[i]]); i = j; } if (reduced_initial.size() > reduced_wanted.size()) { cout << "TAK\n"; return; } if (reduced_initial == reduced_wanted) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool _multipleTestCases = true; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { solve(); } return 0; } |