#include <bits/stdc++.h> using namespace std; int t; int n; int a, b; bool k1, k2, w1, w2; string kolory, wzor; vector<vector<int>> drzewo; vector<bool> visited; string prosta1, prosta2; bool dziala; int v; int p; void DFS(int v) { if (!dziala) return; visited[v] = true; // // ignorować proste z trójek if (drzewo[v].size() <= 2) { prosta1 += kolory[v]; prosta2 += wzor[v]; } if (drzewo[v].size() != 2 && v != p) { // cout << v + 1 << ": " << prosta1 << ' ' << prosta2 << '\n'; char last = prosta1[0]; int l1 = 0; for (auto &c : prosta1) { if (c != last) { last = c; l1++; } } last = prosta1[0]; int l2 = 0; for (auto &c : prosta2) { if (c != last) { last = c; l2++; } } if (l2 > l1) { dziala = false; return; } prosta1 = ""; prosta2 = ""; } for (auto &u : drzewo[v]) { if (visited[u]) continue; DFS(u); } } void proste() { dziala = true; prosta1 = "", prosta2 = ""; p = -1; for (int v = 0; v < n; ++v) { if (drzewo[v].size() == 1) { p = v; break; } } // cout << p + 1 << '\n'; DFS(p); if (!dziala) cout << "NIE\n"; else cout << "TAK\n"; } void solve() { k1 = false, k2 = false, w1 = false, w2 = false; cin >> n; drzewo.resize(0); drzewo.resize(n); visited.assign(n, false); cin >> kolory >> wzor; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; drzewo[a - 1].push_back(b - 1); drzewo[b - 1].push_back(a - 1); } for (auto &c : kolory) { if (c == '0') k1 = true; else k2 = true; } for (auto &c : wzor) { if (c == '0') w1 = true; else w2 = true; } if (w1 && !k1) { cout << "NIE\n"; return; } if (w2 && !k2) { cout << "NIE\n"; return; } for (int v = 0; v < n; ++v) { if (drzewo[v].size() <= 2) continue; for (auto u : drzewo[v]) { if (wzor[v] == wzor[u]) { cout << "TAK\n"; return; } } } for (int v = 0; v < n; ++v) { if (drzewo[v].size() <= 2) continue; // cout << drzewo[v].size() << '\n'; if (kolory[v] != wzor[v]) { cout << "NIE\n"; return; } for (auto u : drzewo[v]) { if (kolory[u] != wzor[u]) { cout << "NIE\n"; return; } } } proste(); // cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { 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 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 | #include <bits/stdc++.h> using namespace std; int t; int n; int a, b; bool k1, k2, w1, w2; string kolory, wzor; vector<vector<int>> drzewo; vector<bool> visited; string prosta1, prosta2; bool dziala; int v; int p; void DFS(int v) { if (!dziala) return; visited[v] = true; // // ignorować proste z trójek if (drzewo[v].size() <= 2) { prosta1 += kolory[v]; prosta2 += wzor[v]; } if (drzewo[v].size() != 2 && v != p) { // cout << v + 1 << ": " << prosta1 << ' ' << prosta2 << '\n'; char last = prosta1[0]; int l1 = 0; for (auto &c : prosta1) { if (c != last) { last = c; l1++; } } last = prosta1[0]; int l2 = 0; for (auto &c : prosta2) { if (c != last) { last = c; l2++; } } if (l2 > l1) { dziala = false; return; } prosta1 = ""; prosta2 = ""; } for (auto &u : drzewo[v]) { if (visited[u]) continue; DFS(u); } } void proste() { dziala = true; prosta1 = "", prosta2 = ""; p = -1; for (int v = 0; v < n; ++v) { if (drzewo[v].size() == 1) { p = v; break; } } // cout << p + 1 << '\n'; DFS(p); if (!dziala) cout << "NIE\n"; else cout << "TAK\n"; } void solve() { k1 = false, k2 = false, w1 = false, w2 = false; cin >> n; drzewo.resize(0); drzewo.resize(n); visited.assign(n, false); cin >> kolory >> wzor; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; drzewo[a - 1].push_back(b - 1); drzewo[b - 1].push_back(a - 1); } for (auto &c : kolory) { if (c == '0') k1 = true; else k2 = true; } for (auto &c : wzor) { if (c == '0') w1 = true; else w2 = true; } if (w1 && !k1) { cout << "NIE\n"; return; } if (w2 && !k2) { cout << "NIE\n"; return; } for (int v = 0; v < n; ++v) { if (drzewo[v].size() <= 2) continue; for (auto u : drzewo[v]) { if (wzor[v] == wzor[u]) { cout << "TAK\n"; return; } } } for (int v = 0; v < n; ++v) { if (drzewo[v].size() <= 2) continue; // cout << drzewo[v].size() << '\n'; if (kolory[v] != wzor[v]) { cout << "NIE\n"; return; } for (auto u : drzewo[v]) { if (kolory[u] != wzor[u]) { cout << "NIE\n"; return; } } } proste(); // cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { solve(); } } |