#define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 1e6+6969, INF = 2e9+6969; int n; vi v[nax], order; string A, B; void clear() { for(int i=1;i<=n;i++) v[i].clear(); order.clear(); } void dfs(int a, int dad) { order.pb(a); for(auto e: v[a]) { if(e == dad) continue; dfs(e, a); } } void test_case() { scanf("%d", &n); cin >> A >> B; bool same = 0, one_col = 1; for(int i=0;i<n;i++) if(A[i] != A[0]) one_col = 0; for(int i=0;i<n-1;i++) { int a, b; scanf("%d%d", &a, &b); v[a].pb(b); v[b].pb(a); if(B[a-1] == B[b-1]) same = 1; } if(A == B) { puts("TAK"); clear(); return; } if(one_col) { puts("NIE"); clear(); return; } if(!same) { puts("NIE"); clear(); return; } bool ok = 0; for(int i=1;i<=n;i++) { if(v[i].size() > 2) { ok = 1; break; } } if(ok) { puts("TAK"); clear(); return; } // ścieżka :weary: int st = 1; for(int i=1;i<=n;i++) if(v[i].size() == 1) { st = i; break; } dfs(st, st); vi x, y; int cnt = 0; char last = '0'; for(auto e: order) { if(last != A[e-1]) { x.pb(cnt); cnt = 0; } cnt++; last = A[e-1]; } x.pb(cnt); cnt = 0; last = '0'; for(auto e: order) { if(last != B[e-1]) { y.pb(cnt); cnt = 0; } cnt++; last = B[e-1]; } y.pb(cnt); // for(auto e: x) printf("%d ", e); // puts(""); // for(auto e: y) printf("%d ", e); // puts(""); if(y.size() > x.size() || (x[0] == 0 && y[0] != 0 && x.size() - 2 < y.size())) puts("NIE"); else puts("TAK"); clear(); } int main() { int qq; scanf("%d", &qq); while(qq--) test_case(); 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 | #define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 1e6+6969, INF = 2e9+6969; int n; vi v[nax], order; string A, B; void clear() { for(int i=1;i<=n;i++) v[i].clear(); order.clear(); } void dfs(int a, int dad) { order.pb(a); for(auto e: v[a]) { if(e == dad) continue; dfs(e, a); } } void test_case() { scanf("%d", &n); cin >> A >> B; bool same = 0, one_col = 1; for(int i=0;i<n;i++) if(A[i] != A[0]) one_col = 0; for(int i=0;i<n-1;i++) { int a, b; scanf("%d%d", &a, &b); v[a].pb(b); v[b].pb(a); if(B[a-1] == B[b-1]) same = 1; } if(A == B) { puts("TAK"); clear(); return; } if(one_col) { puts("NIE"); clear(); return; } if(!same) { puts("NIE"); clear(); return; } bool ok = 0; for(int i=1;i<=n;i++) { if(v[i].size() > 2) { ok = 1; break; } } if(ok) { puts("TAK"); clear(); return; } // ścieżka :weary: int st = 1; for(int i=1;i<=n;i++) if(v[i].size() == 1) { st = i; break; } dfs(st, st); vi x, y; int cnt = 0; char last = '0'; for(auto e: order) { if(last != A[e-1]) { x.pb(cnt); cnt = 0; } cnt++; last = A[e-1]; } x.pb(cnt); cnt = 0; last = '0'; for(auto e: order) { if(last != B[e-1]) { y.pb(cnt); cnt = 0; } cnt++; last = B[e-1]; } y.pb(cnt); // for(auto e: x) printf("%d ", e); // puts(""); // for(auto e: y) printf("%d ", e); // puts(""); if(y.size() > x.size() || (x[0] == 0 && y[0] != 0 && x.size() - 2 < y.size())) puts("NIE"); else puts("TAK"); clear(); } int main() { int qq; scanf("%d", &qq); while(qq--) test_case(); return 0; } |