#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SZ(a) ((int)(a).size())
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
constexpr int kInf = 1000 * 1000 * 1000 + 7;
constexpr int kMod = 1000 * 1000 * 1000 + 7;
constexpr long long kInfLL = 1e18;
vi GetPath(const vvi& g) {
vi p;
REP(i, SZ(g)) if (SZ(g[i]) == 1) {
p.push_back(i);
break;
}
int prv = -1;
while (SZ(p) < SZ(g)) {
int lst = p.back();
for (int to: g[lst]) if (to != prv) {
p.push_back(to);
break;
}
prv = lst;
}
return p;
}
vi GetSeq(const vvi& g, const string& c) {
const int n = SZ(g);
vi rs;
vi p = GetPath(g);
REP(i, n) rs.push_back(c[p[i]]);
return rs;
}
bool IsSubseq(const vi& a, const vi& b) {
int i = 0;
for (int e: b) {
while (i < SZ(a) && a[i] != e) ++i;
if (i == SZ(a)) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
REP(i, t) {
int n;
cin >> n;
string a, b;
cin >> a >> b;
vvi g(n);
REP(i, n - 1) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back(y);
g[y].push_back(x);
}
if (a == b) {
cout << "TAK\n";
continue;
}
bool same = count(ALL(a), a[0]) == n;
if (same) {
cout << "NIE\n";
continue;
}
bool diff = false;
REP(v, n) for (int to: g[v]) if (b[v] == b[to]) diff = true;
if (!diff) {
cout << "NIE\n";
continue;
}
int mx = 0;
REP(i, n) mx = max<int>(mx, g[i].size());
if (mx >= 3) {
cout << "TAK\n";
continue;
}
cout << (IsSubseq(GetSeq(g, a), GetSeq(g, b)) ? "TAK" : "NIE") << '\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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; vi GetPath(const vvi& g) { vi p; REP(i, SZ(g)) if (SZ(g[i]) == 1) { p.push_back(i); break; } int prv = -1; while (SZ(p) < SZ(g)) { int lst = p.back(); for (int to: g[lst]) if (to != prv) { p.push_back(to); break; } prv = lst; } return p; } vi GetSeq(const vvi& g, const string& c) { const int n = SZ(g); vi rs; vi p = GetPath(g); REP(i, n) rs.push_back(c[p[i]]); return rs; } bool IsSubseq(const vi& a, const vi& b) { int i = 0; for (int e: b) { while (i < SZ(a) && a[i] != e) ++i; if (i == SZ(a)) return false; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; REP(i, t) { int n; cin >> n; string a, b; cin >> a >> b; vvi g(n); REP(i, n - 1) { int x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } if (a == b) { cout << "TAK\n"; continue; } bool same = count(ALL(a), a[0]) == n; if (same) { cout << "NIE\n"; continue; } bool diff = false; REP(v, n) for (int to: g[v]) if (b[v] == b[to]) diff = true; if (!diff) { cout << "NIE\n"; continue; } int mx = 0; REP(i, n) mx = max<int>(mx, g[i].size()); if (mx >= 3) { cout << "TAK\n"; continue; } cout << (IsSubseq(GetSeq(g, a), GetSeq(g, b)) ? "TAK" : "NIE") << '\n'; } } |
English