// Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); const int N = (int)1e5 + 3; int tt, n, oj[N], siz[N]; string s, t; bool ma[N][2], lol; VI child[N]; VII edg; void dfs0(int v, int par) { oj[v] = par; siz[v] = 1; if (par) ma[v][t[par-1]-'0'] = true; for (int x: child[v]) if (x != par) { ma[v][t[x-1]-'0'] = true; dfs0(x, v); siz[v] += siz[x]; if (ma[x][1-(t[x-1]-'0')]) ma[v][1-(t[x-1]-'0')] = true; } } void dfs(int v, int par, VI& v1, VI& v2) { if (v1.empty() || v1.back() != s[v-1]-'0') v1.PB(s[v-1]-'0'); if (v2.empty() || v2.back() != t[v-1]-'0') v2.PB(t[v-1]-'0'); for (int x: child[v]) if (x != par) dfs(x, v, v1, v2); } bool git(int v) { bool is[2] = {false, false}; for (int x: child[v]) is[t[x-1]-'0'] = true; if ((is[0] && is[1]) || t[v-1] == t[child[v].back()-1] || lol) return true; return false; for (int x: child[v]) { // int si = siz[x]; // if (x == oj[v]) // si = n-siz[v]; // if (si > 2) // return true; if (ma[x][1-(t[v-1]-'0')]) return true; } return false; } int main() { init_ios(); // for (int te = 1; te <= 500; ++te) { // cout<<"test "<<te<<"\n"; // string inps = "in/dcc" + to_string(te) + ".in"; // string outs = "out/dcc" + to_string(te) + ".out"; // ifstream in(inps.c_str()); // ifstream out(outs.c_str()); cin >> tt; while (tt--) { cin >> n >> s >> t; for (int i = 1; i <= n; ++i) { ma[i][0] = ma[i][1] = false; child[i].clear(); } lol = false; edg.clear(); vector<int> is[2]; is[0].PB(0); is[0].PB(0); is[1].PB(0); is[1].PB(0); for (char c: s) is[0][c-'0']++; for (char c: t) is[1][c-'0']++; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; edg.PB({u, v}); child[u].PB(v); child[v].PB(u); if (t[u-1] == t[v-1]) lol = true; } dfs0(1, 0); string res = ""; bool big = false, ok = false; int hm = 0, v = -1; int pt1 = 0, pt2 = 0; VI v1, v2; if (s == t) { res = "TAK"; goto qwe; } if ((is[0][0] == 0 && is[1][0] > 0) || (is[0][1] == 0 && is[1][1] > 0)) { // cout<<"NIE\n"; res = "NIE"; goto qwe; // continue; } if (is[0][0] == 0 || is[0][1] == 0 || is[1][0] == 0 || is[1][1] == 0) { // cout<<"TAK\n"; res = "TAK"; goto qwe; // continue; } for (int i = 1; i <= n; ++i) { if (child[i].size() > 2) { big = true; ok |= git(i); } if (child[i].size() == 1) { ++hm; v = i; } } if (big) { if (ok) { res = "TAK"; // cout<<"TAK\n"; } else { res = "NIE"; // cout<<"NIE\n"; } goto qwe; // continue; } assert(hm == 2); dfs(v, -1, v1, v2); while (pt1 < (int)v1.size() && pt2 < (int)v2.size()) { if (v1[pt1] == v2[pt2]) ++pt2; ++pt1; } if (pt2 == (int)v2.size()) { res = "TAK"; // cout<<"TAK\n"; } else { res = "NIE"; // cout<<"NIE\n"; } qwe:; // string exp; // out >> exp; // if (exp != res) { // cout<<"exp: "<<exp<<", moj: "<<res<<"\n"; // cout<<"Jest:\n"; // cout<<n<<"\n"<<s<<"\n"<<t<<"\n"; // for (PII ed: edg) // cout<<ed.ST<<" "<<ed.ND<<"\n"; // return 0; // } cout<<res<<"\n"; } // } }
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 185 186 187 188 189 190 191 192 193 194 195 196 197 | // Daniel Grzegorzewski // while (clock()<=69*CLOCKS_PER_SEC) #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") #define MP make_pair #define PB push_back #define ST first #define ND second using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; void init_ios() { ios_base::sync_with_stdio(0); cin.tie(0); } auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); const int N = (int)1e5 + 3; int tt, n, oj[N], siz[N]; string s, t; bool ma[N][2], lol; VI child[N]; VII edg; void dfs0(int v, int par) { oj[v] = par; siz[v] = 1; if (par) ma[v][t[par-1]-'0'] = true; for (int x: child[v]) if (x != par) { ma[v][t[x-1]-'0'] = true; dfs0(x, v); siz[v] += siz[x]; if (ma[x][1-(t[x-1]-'0')]) ma[v][1-(t[x-1]-'0')] = true; } } void dfs(int v, int par, VI& v1, VI& v2) { if (v1.empty() || v1.back() != s[v-1]-'0') v1.PB(s[v-1]-'0'); if (v2.empty() || v2.back() != t[v-1]-'0') v2.PB(t[v-1]-'0'); for (int x: child[v]) if (x != par) dfs(x, v, v1, v2); } bool git(int v) { bool is[2] = {false, false}; for (int x: child[v]) is[t[x-1]-'0'] = true; if ((is[0] && is[1]) || t[v-1] == t[child[v].back()-1] || lol) return true; return false; for (int x: child[v]) { // int si = siz[x]; // if (x == oj[v]) // si = n-siz[v]; // if (si > 2) // return true; if (ma[x][1-(t[v-1]-'0')]) return true; } return false; } int main() { init_ios(); // for (int te = 1; te <= 500; ++te) { // cout<<"test "<<te<<"\n"; // string inps = "in/dcc" + to_string(te) + ".in"; // string outs = "out/dcc" + to_string(te) + ".out"; // ifstream in(inps.c_str()); // ifstream out(outs.c_str()); cin >> tt; while (tt--) { cin >> n >> s >> t; for (int i = 1; i <= n; ++i) { ma[i][0] = ma[i][1] = false; child[i].clear(); } lol = false; edg.clear(); vector<int> is[2]; is[0].PB(0); is[0].PB(0); is[1].PB(0); is[1].PB(0); for (char c: s) is[0][c-'0']++; for (char c: t) is[1][c-'0']++; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; edg.PB({u, v}); child[u].PB(v); child[v].PB(u); if (t[u-1] == t[v-1]) lol = true; } dfs0(1, 0); string res = ""; bool big = false, ok = false; int hm = 0, v = -1; int pt1 = 0, pt2 = 0; VI v1, v2; if (s == t) { res = "TAK"; goto qwe; } if ((is[0][0] == 0 && is[1][0] > 0) || (is[0][1] == 0 && is[1][1] > 0)) { // cout<<"NIE\n"; res = "NIE"; goto qwe; // continue; } if (is[0][0] == 0 || is[0][1] == 0 || is[1][0] == 0 || is[1][1] == 0) { // cout<<"TAK\n"; res = "TAK"; goto qwe; // continue; } for (int i = 1; i <= n; ++i) { if (child[i].size() > 2) { big = true; ok |= git(i); } if (child[i].size() == 1) { ++hm; v = i; } } if (big) { if (ok) { res = "TAK"; // cout<<"TAK\n"; } else { res = "NIE"; // cout<<"NIE\n"; } goto qwe; // continue; } assert(hm == 2); dfs(v, -1, v1, v2); while (pt1 < (int)v1.size() && pt2 < (int)v2.size()) { if (v1[pt1] == v2[pt2]) ++pt2; ++pt1; } if (pt2 == (int)v2.size()) { res = "TAK"; // cout<<"TAK\n"; } else { res = "NIE"; // cout<<"NIE\n"; } qwe:; // string exp; // out >> exp; // if (exp != res) { // cout<<"exp: "<<exp<<", moj: "<<res<<"\n"; // cout<<"Jest:\n"; // cout<<n<<"\n"<<s<<"\n"<<t<<"\n"; // for (PII ed: edg) // cout<<ed.ST<<" "<<ed.ND<<"\n"; // return 0; // } cout<<res<<"\n"; } // } } |