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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define iamspeed ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
int z, maxw, n, w[100005], ob[100005], doc[100005], vis[100005], a, b;
string s1, s2;
vector<int> G[100005], A, B;

void DFS(int w, int ind)
{
    vis[w] = 1;
    ob[ind] = (int)(s1[w - 1] - '0');
    doc[ind] = (int)(s2[w - 1] - '0');
    for(int i = 0 ; i < G[w].size() ; i++)
    {
        if(!vis[G[w][i]]) DFS(G[w][i], ind + 1);
    }
}

int main()
{
    iamspeed;
    cin >> z;
    for(int k = 1 ; k <= z ; k++)
    {
        A.clear() ; B.clear();
        maxw = 0;
        cin >> n;
        for(int i = 1 ; i <= n ; i++)
        {
            w[i] = vis[i] = 0;
            G[i].clear();
        }
        cin >> s1 >> s2;
        for(int i = 1 ; i < n ; i++)
        {
            cin >> a >> b;
            G[a].push_back(b);
            G[b].push_back(a);
            w[a]++;
            w[b]++;
            maxw = max(maxw, max(w[a], w[b]));
        }
        if(maxw < 3)
        {
            for(int i = 1 ; i <= n ; i++)
            {
                if(w[i] == 1)
                {
                    DFS(i, 1);
                    break;
                }
            }
            ob[0] = doc[0] = -1;
            for(int i = 1 ; i <= n ; i++)
            {
                if(ob[i] != ob[i - 1]) A.push_back(ob[i]);
                if(doc[i] != doc[i - 1]) B.push_back(doc[i]);
            }
            if(A[0] != B[0]) B.push_back(1);
            if(A.size() >= B.size())
            {
                cout << "TAK" << endl;
            }
            else
            {
                cout << "NIE" << endl;
            }
        }
        else
        {
            cout << "TAK" << endl;
        }

    }
}