#include <bits/stdc++.h> #include <chrono> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace chrono; using namespace __gnu_pbds; #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") #pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native") // for szkopul and sio only typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; #define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase( unique(all(x)), (x).end()) #define popcnt(x) __builtin_popcount(x) #define sz size() #define time_since duration_cast< milliseconds >(system_clock::now().time_since_epoch()) template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } const int N = 1e6 + 1; char s[2][N]; vector<int>c[N]; pii dp[4][N]; pii operator+ (pii a, pii b) { return {a.ff + b.ff, a.dd + b.dd}; } pii operator- (pii a, pii b) { return {a.ff - b.ff, a.dd - b.dd}; } void dfs(int x, int p = -1) { For(i, 0, 4) dp[i][x] = mp(0, 0); for(auto ss : c[x]) if(ss != p) { dfs(ss, x); /*dp[0][x] = max(dp[0][x], dp[0][ss] + (s[0][x] != s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0))); dp[1][x] = max(dp[1][x], dp[1][ss] + (s[0][x] != s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0))); dp[2][x] = max(dp[2][x], dp[2][ss] + (s[1][x] != s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0))); dp[3][x] = max(dp[3][x], dp[3][ss] + (s[1][x] != s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)));*/ dp[0][x] = dp[0][x] + dp[0][ss] - (s[0][x] == s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0)); dp[1][x] = dp[1][x] + dp[1][ss] - (s[0][x] == s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0)); dp[2][x] = dp[2][x] + dp[2][ss] - (s[1][x] == s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)); dp[3][x] = dp[3][x] + dp[3][ss] - (s[1][x] == s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)); if(dp[0][x].ff < dp[1][x].ff) dp[0][x] = dp[1][x]; if(dp[1][x].dd < dp[0][x].dd) dp[1][x] = dp[0][x]; if(dp[2][x].ff < dp[3][x].ff) dp[2][x] = dp[3][x]; if(dp[3][x].dd < dp[2][x].dd) dp[3][x] = dp[2][x]; } if(c[x].sz == 1 && c[x][0] == p) { dp[0][x] = mp(1 ^ (s[0][x] & 1), s[0][x] & 1); dp[1][x] = dp[0][x]; dp[2][x] = mp(1 ^ (s[1][x] & 1), s[1][x] & 1); dp[3][x] = dp[2][x]; } } bool check(int root) { dfs(root); bool ok = 1; /*For(x, 1, n + 1) { For(i, 0, 4) cout << dp[i][x] << " "; puts(""); }*/ if(dp[0][root].ff + dp[0][root].dd < dp[2][root].ff + dp[2][root].dd) ok = 0; if(dp[1][root].ff + dp[1][root].dd < dp[3][root].ff + dp[3][root].dd) ok = 0; if(dp[0][root].ff + dp[0][root].dd == dp[2][root].ff + dp[2][root].dd && s[0][root] != s[1][root]) ok = 0; if(dp[1][root].ff + dp[1][root].dd == dp[3][root].ff + dp[3][root].dd && s[0][root] != s[1][root]) ok = 0; return ok; } void solve(void) { int n; scanf("%d", &n); For(i, 0, 2) scanf("%s", s[i] + 1); For(i, 1, n + 1) { c[i].clear(); For(j, 0, 4) dp[j][i] = {0, 0}; } For(i, 0, n - 1) { int g, h; scanf("%d%d", &g, &h); c[g].pb(h); c[h].pb(g); } bool pres[4] = {0, 0, 0, 0}; For(i, 1, n + 1) pres[s[0][i] - '0'] = 1; For(i, 1, n + 1) pres[2 + s[1][i] - '0'] = 1; if(pres[0] + pres[1] < pres[2] + pres[3]) { puts("NIE"); return; } int rot[2] = {-1, -1}; For(i, 1, n + 1) if(s[0][i] == s[1][i]) { rot[s[0][i] & 1] = i; } /*if(rot[0] == -1 && rot[1] == -1) { puts("NIEE"); return; }*/ //if(rot[0] >= 0 && rot[1] >= 1) { For(i, 1, n + 1) if(c[i].sz >= 3) { //puts(check(i) ? "TAK" : "NIE"); bool helpnieumiemdcc[2] = {0, 0}; for(auto ss : c[i]) helpnieumiemdcc[s[1][ss] - '0'] = 1; if(helpnieumiemdcc[1] + helpnieumiemdcc[0] == 2) { puts("TAK"); return; } if(check(i)) { puts("TAK"); return; } } //}*/ //int root = 1;// rot[0] == -1 ? rot[1] : rot[0]; /* For(i, 1, n + 1) if(c[i].sz == 1) root = i;*/ bool ok = 0; int ct = 0; For(root, 1, n + 1) { //cout << root << endl; if(c[root].sz < 2) { ok |= check(root); ++ct; if(ok) break; if(ct > 10) break; } } puts(ok ? "TAK" : "NIE"); } int32_t main(void){ int t = 1; scanf("%d", &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 174 175 176 177 | #include <bits/stdc++.h> #include <chrono> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace chrono; using namespace __gnu_pbds; #pragma GCC optimize("Ofast,unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") #pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native") // for szkopul and sio only typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; #define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin(), (x).end() #define make_unique(x) (x).erase( unique(all(x)), (x).end()) #define popcnt(x) __builtin_popcount(x) #define sz size() #define time_since duration_cast< milliseconds >(system_clock::now().time_since_epoch()) template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } const int N = 1e6 + 1; char s[2][N]; vector<int>c[N]; pii dp[4][N]; pii operator+ (pii a, pii b) { return {a.ff + b.ff, a.dd + b.dd}; } pii operator- (pii a, pii b) { return {a.ff - b.ff, a.dd - b.dd}; } void dfs(int x, int p = -1) { For(i, 0, 4) dp[i][x] = mp(0, 0); for(auto ss : c[x]) if(ss != p) { dfs(ss, x); /*dp[0][x] = max(dp[0][x], dp[0][ss] + (s[0][x] != s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0))); dp[1][x] = max(dp[1][x], dp[1][ss] + (s[0][x] != s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0))); dp[2][x] = max(dp[2][x], dp[2][ss] + (s[1][x] != s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0))); dp[3][x] = max(dp[3][x], dp[3][ss] + (s[1][x] != s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)));*/ dp[0][x] = dp[0][x] + dp[0][ss] - (s[0][x] == s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0)); dp[1][x] = dp[1][x] + dp[1][ss] - (s[0][x] == s[0][ss] ? mp(1 ^ (s[0][x] & 1), s[0][x] & 1): mp(0, 0)); dp[2][x] = dp[2][x] + dp[2][ss] - (s[1][x] == s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)); dp[3][x] = dp[3][x] + dp[3][ss] - (s[1][x] == s[1][ss] ? mp(1 ^ (s[1][x] & 1), s[1][x] & 1): mp(0, 0)); if(dp[0][x].ff < dp[1][x].ff) dp[0][x] = dp[1][x]; if(dp[1][x].dd < dp[0][x].dd) dp[1][x] = dp[0][x]; if(dp[2][x].ff < dp[3][x].ff) dp[2][x] = dp[3][x]; if(dp[3][x].dd < dp[2][x].dd) dp[3][x] = dp[2][x]; } if(c[x].sz == 1 && c[x][0] == p) { dp[0][x] = mp(1 ^ (s[0][x] & 1), s[0][x] & 1); dp[1][x] = dp[0][x]; dp[2][x] = mp(1 ^ (s[1][x] & 1), s[1][x] & 1); dp[3][x] = dp[2][x]; } } bool check(int root) { dfs(root); bool ok = 1; /*For(x, 1, n + 1) { For(i, 0, 4) cout << dp[i][x] << " "; puts(""); }*/ if(dp[0][root].ff + dp[0][root].dd < dp[2][root].ff + dp[2][root].dd) ok = 0; if(dp[1][root].ff + dp[1][root].dd < dp[3][root].ff + dp[3][root].dd) ok = 0; if(dp[0][root].ff + dp[0][root].dd == dp[2][root].ff + dp[2][root].dd && s[0][root] != s[1][root]) ok = 0; if(dp[1][root].ff + dp[1][root].dd == dp[3][root].ff + dp[3][root].dd && s[0][root] != s[1][root]) ok = 0; return ok; } void solve(void) { int n; scanf("%d", &n); For(i, 0, 2) scanf("%s", s[i] + 1); For(i, 1, n + 1) { c[i].clear(); For(j, 0, 4) dp[j][i] = {0, 0}; } For(i, 0, n - 1) { int g, h; scanf("%d%d", &g, &h); c[g].pb(h); c[h].pb(g); } bool pres[4] = {0, 0, 0, 0}; For(i, 1, n + 1) pres[s[0][i] - '0'] = 1; For(i, 1, n + 1) pres[2 + s[1][i] - '0'] = 1; if(pres[0] + pres[1] < pres[2] + pres[3]) { puts("NIE"); return; } int rot[2] = {-1, -1}; For(i, 1, n + 1) if(s[0][i] == s[1][i]) { rot[s[0][i] & 1] = i; } /*if(rot[0] == -1 && rot[1] == -1) { puts("NIEE"); return; }*/ //if(rot[0] >= 0 && rot[1] >= 1) { For(i, 1, n + 1) if(c[i].sz >= 3) { //puts(check(i) ? "TAK" : "NIE"); bool helpnieumiemdcc[2] = {0, 0}; for(auto ss : c[i]) helpnieumiemdcc[s[1][ss] - '0'] = 1; if(helpnieumiemdcc[1] + helpnieumiemdcc[0] == 2) { puts("TAK"); return; } if(check(i)) { puts("TAK"); return; } } //}*/ //int root = 1;// rot[0] == -1 ? rot[1] : rot[0]; /* For(i, 1, n + 1) if(c[i].sz == 1) root = i;*/ bool ok = 0; int ct = 0; For(root, 1, n + 1) { //cout << root << endl; if(c[root].sz < 2) { ok |= check(root); ++ct; if(ok) break; if(ct > 10) break; } } puts(ok ? "TAK" : "NIE"); } int32_t main(void){ int t = 1; scanf("%d", &t); while(t--) solve(); } |