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
145
146
147
148
149
150
151
152
153
154
#include <bits/stdc++.h>

using namespace std;

struct xd
{
    int z;
    int j;
    int d;
};

char x;
int z,n,a,b;
xd t1[100001];
xd t2[100001];
bitset <100001> b1;
bitset <100001> b2;
vector <int> v[100001];

xd dodaj(xd a, xd b)
{
    xd tmp;
    tmp.z=a.z+b.z;
    tmp.j=a.j+b.j;
    tmp.d=a.d+b.d;
    return tmp;
}

void dfs(int w, int p)
{
    for (auto i : v[w])
    {
        if (i==p) continue;
        else dfs(i,w);
        t1[w]=dodaj(t1[w],t1[i]);
        t2[w]=dodaj(t2[w],t2[i]);
    }
    bool d=0;
    if (v[w].size()==1 && w!=1)
    {
        t1[w].z=0;
        t1[w].j=0;
        t1[w].d=0;
        t2[w].z=0;
        t2[w].j=0;
        t2[w].d=0;
        if (b1[w]) t1[w].j++;
        else t1[w].z++;
        if (b2[w]) t2[w].j++;
        else t2[w].z++;
        return;
    }
    if (t1[w].d==0)
    {
        if (t2[w].j == 0)
        {
            if (t1[w].j == 0)
            {
                if (b1[w]==1) d=1;
            }
            else if (t1[w].z==0)
            {
                b2[w]=1;
            }
            else b1[w]=0;
        }
        else if (t2[w].z == 0)
        {
            if (t1[w].z == 0)
            {
                if (b1[w]==0) d=1;
            }
            else if (t1[w].j==0)
            {
                b2[w]=0;
            }
            else b1[w]=1;
        }
        else
        {
            if (t1[w].z==0)
            {
                b2[w]=0;
            }
            else if (t2[w].j==0)
            {
                b2[w]=1;
            }
            else d=1; 
        }
    }
    else
    {
        if (t2[w].z==0)
        {
            if (t1[w].z==0)
            {
                if (b1[w]==0) d=1;                
            }
            else b1[w]=1;
        }
        else if (t2[w].j==0)
        {
            if (t1[w].j==0)
            {
                if (b1[w]==1) d=1;                
            }
            else b1[w]=0;
        }
        else d=1;
    }
    t1[w].z=0;
    t1[w].j=0;
    t1[w].d=0;
    t2[w].z=0;
    t2[w].j=0;
    t2[w].d=0;
    if (d) t1[w].d++;
    else if (b1[w]) t1[w].j++;
    else t1[w].z++;
    if (b2[w]) t2[w].j++;
    else t2[w].z++;
}

int main()
{
    scanf("%d",&z);
    for (int zz=1; zz<=z; zz++)
    {
        scanf("%d",&n);
        for (int i=1; i<=n; i++)
        {
            scanf(" %c",&x);
            if (x == '0') b1[i]=0;
            else b1[i]=1;
            v[i].clear();
        }
        for (int i=1; i<=n; i++)
        {
            scanf(" %c",&x);
            if (x == '0') b2[i]=0;
            else b2[i]=1;
        }
        for (int i=1; i<n; i++)
        {
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
        }
        dfs(1,1);
        if (b1[1]==b2[1]) printf("TAK\n");
        else printf("NIE\n");
    }
}