// Karol Kosinski 2021 #include <bits/stdc++.h> // #define ENABLE_DEBUG #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,f,x...) #else #define DEB(k,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);f(x);} #endif #define DEBL DEB(1,void,0) #define DEBF(f,x...) DEB(1,f,x) #define DEBUG(x...) DEB(0,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 100'003; vector<int> G[NX]; char S1[NX], S2[NX]; bool is_line(int n) { FOR(i,0,n) { DEBUG("** %d\n", SIZE( G[i] )); if ( SIZE( G[i] ) > 2 ) return false; } return true; } bool is_all(int n, char* S, char c) { FOR(i,0,n) if ( S[i] != c ) return false; return true; } bool is_2colored(int n, char* S) { FOR(u,0,n) { for ( int v : G[u] ) if ( S[u] == S[v] ) return false; } return true; } bool is_same_colored(int n, char* S1, char* S2) { FOR(i,0,n) if ( S1[i] != S2[i] ) return false; return true; } int find_start(int n) { FOR(i,0,n) if ( SIZE( G[i] ) <= 1 ) return i; return -1; } bool testcase() { int n; scanf("%d%s%s", &n, S1, S2); FOR(i,0,n) { G[i].clear(); G[i].shrink_to_fit(); } FOR(i,1,n) { int a, b; scanf("%d%d", &a, &b); -- a, -- b; G[a].push_back(b); G[b].push_back(a); } if ( not is_line(n) ) { DEBUG("not line\n"); bool a0 = is_all(n, S1, '0'); bool a1 = is_all(n, S1, '1'); bool b0 = is_all(n, S2, '0'); bool b1 = is_all(n, S2, '1'); if (a0) { return b0; } else if (a1) { return b1; } else { if ( is_2colored(n, S2) ) return is_same_colored(n, S1, S2); else return true; } } int u = find_start(n), prev = -1; DEBUG("line: %d\n", u); vector<char> V1, V2; V1.push_back( S1[u] ); V2.push_back( S2[u] ); for (;;) { DEBUG("%d ", u); bool ex = true; for ( int v : G[u] ) { DEBUG("( %d [%d] %d ) ", prev, u, v); if ( v == prev ) continue; if ( V1.back() != S1[v] ) V1.push_back( S1[v] ); if ( V2.back() != S2[v] ) V2.push_back( S2[v] ); prev = u; u = v; ex = false; break; } if (ex) break; } DEBUG("\n"); DEBUG("** 1 front: %c size: %2d\n", V1.front(), SIZE(V1) ); DEBUG("** 2 front: %c size: %2d\n", V2.front(), SIZE(V2) ); if ( V1.front() == V2.front() ) { return SIZE(V1) >= SIZE(V2); } return SIZE(V1) > SIZE(V2); } int main() { int z; scanf("%d", &z); while ( z -- ) printf( testcase() ? "TAK\n" : "NIE\n" ); 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 | // Karol Kosinski 2021 #include <bits/stdc++.h> // #define ENABLE_DEBUG #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,f,x...) #else #define DEB(k,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);f(x);} #endif #define DEBL DEB(1,void,0) #define DEBF(f,x...) DEB(1,f,x) #define DEBUG(x...) DEB(0,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 100'003; vector<int> G[NX]; char S1[NX], S2[NX]; bool is_line(int n) { FOR(i,0,n) { DEBUG("** %d\n", SIZE( G[i] )); if ( SIZE( G[i] ) > 2 ) return false; } return true; } bool is_all(int n, char* S, char c) { FOR(i,0,n) if ( S[i] != c ) return false; return true; } bool is_2colored(int n, char* S) { FOR(u,0,n) { for ( int v : G[u] ) if ( S[u] == S[v] ) return false; } return true; } bool is_same_colored(int n, char* S1, char* S2) { FOR(i,0,n) if ( S1[i] != S2[i] ) return false; return true; } int find_start(int n) { FOR(i,0,n) if ( SIZE( G[i] ) <= 1 ) return i; return -1; } bool testcase() { int n; scanf("%d%s%s", &n, S1, S2); FOR(i,0,n) { G[i].clear(); G[i].shrink_to_fit(); } FOR(i,1,n) { int a, b; scanf("%d%d", &a, &b); -- a, -- b; G[a].push_back(b); G[b].push_back(a); } if ( not is_line(n) ) { DEBUG("not line\n"); bool a0 = is_all(n, S1, '0'); bool a1 = is_all(n, S1, '1'); bool b0 = is_all(n, S2, '0'); bool b1 = is_all(n, S2, '1'); if (a0) { return b0; } else if (a1) { return b1; } else { if ( is_2colored(n, S2) ) return is_same_colored(n, S1, S2); else return true; } } int u = find_start(n), prev = -1; DEBUG("line: %d\n", u); vector<char> V1, V2; V1.push_back( S1[u] ); V2.push_back( S2[u] ); for (;;) { DEBUG("%d ", u); bool ex = true; for ( int v : G[u] ) { DEBUG("( %d [%d] %d ) ", prev, u, v); if ( v == prev ) continue; if ( V1.back() != S1[v] ) V1.push_back( S1[v] ); if ( V2.back() != S2[v] ) V2.push_back( S2[v] ); prev = u; u = v; ex = false; break; } if (ex) break; } DEBUG("\n"); DEBUG("** 1 front: %c size: %2d\n", V1.front(), SIZE(V1) ); DEBUG("** 2 front: %c size: %2d\n", V2.front(), SIZE(V2) ); if ( V1.front() == V2.front() ) { return SIZE(V1) >= SIZE(V2); } return SIZE(V1) > SIZE(V2); } int main() { int z; scanf("%d", &z); while ( z -- ) printf( testcase() ? "TAK\n" : "NIE\n" ); return 0; } |