#include <bits/stdc++.h>
using namespace std;
struct ed
{
int a, b;
};
bool operator < (ed x, ed y)
{
return x.a < y.a || (x.a == y.a && x.b < y.b);
}
int n, m;
ed t[200001];
char t2[100001], ct[100001];
void in()
{
scanf("%d", &n);
scanf("%s%s", ct + 1, t2 + 1);
m = n - 1;
for(int i = 0; i < m; i++)
{
scanf("%d%d", &t[i].a, &t[i].b);
t[i + m] = {t[i].b, t[i].a};
}
m <<= 1;
}
bool check()
{
for(int i = 1; i <= n; i++)
{
if(ct[i] ^ t2[i])
return 0;
}
return 1;
}
bool komb(int s)
{
if(check())
return 1;
for(int i = 0; i < s; i++)
{
char tp = ct[t[i].b];
ct[t[i].b] = ct[t[i].a];
swap(t[i], t[s - 1]);
if(komb(s - 1))
return 1;
swap(t[i], t[s - 1]);
ct[t[i].b] = tp;
}
return 0;
}
int main()
{
int q;
scanf("%d", &q);
while(q--)
{
in();
if(komb(m))
printf("TAK\n");
else
printf("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 | #include <bits/stdc++.h> using namespace std; struct ed { int a, b; }; bool operator < (ed x, ed y) { return x.a < y.a || (x.a == y.a && x.b < y.b); } int n, m; ed t[200001]; char t2[100001], ct[100001]; void in() { scanf("%d", &n); scanf("%s%s", ct + 1, t2 + 1); m = n - 1; for(int i = 0; i < m; i++) { scanf("%d%d", &t[i].a, &t[i].b); t[i + m] = {t[i].b, t[i].a}; } m <<= 1; } bool check() { for(int i = 1; i <= n; i++) { if(ct[i] ^ t2[i]) return 0; } return 1; } bool komb(int s) { if(check()) return 1; for(int i = 0; i < s; i++) { char tp = ct[t[i].b]; ct[t[i].b] = ct[t[i].a]; swap(t[i], t[s - 1]); if(komb(s - 1)) return 1; swap(t[i], t[s - 1]); ct[t[i].b] = tp; } return 0; } int main() { int q; scanf("%d", &q); while(q--) { in(); if(komb(m)) printf("TAK\n"); else printf("NIE\n"); } } |
English