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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//Krzysztof Pieprzak
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) auto v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(auto i : (c))
#define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);

#define debugOn 0
#define debug(a,b) if (debugOn) cout << a << ": " << b << endl;

const int    INF = 1000000001;
const double EPS = 10e-9;

const int MAXN = 100000;

vi t[MAXN + 5];
char inputColors[MAXN + 5];
char outputColors[MAXN + 5];
int n = 0;

bool vis[MAXN + 5];
int path[MAXN + 5];

void cleanup() {
    FOR (i, 0, n) {
        t[i].clear();
        vis[i] = 0;
    }
}

void read() {
    scanf("%d", &n);
    scanf("%s", inputColors);
    scanf("%s", outputColors);

    int a, b;
    FOR (i, 1, n) {
        scanf("%d %d", &a, &b);
        --a; --b;
        t[a].PUB(b);
        t[b].PUB(a);
    }
}

int getColorsHash(char *colors) {
    int colorsHash = 0;
    FOR (i, 0, n) {
        if (colors[i] == '0') {
            colorsHash |= 1;
        } else {
            colorsHash |= 2;
        }

        if (colorsHash == 3) {
            return colorsHash;
        }
    }

    return colorsHash;
}

bool containsThreeDegVertex() {
    FOR (i, 0, n) {
        if (Size(t[i]) > 2) {
            return true;
        }
    }

    return false;
}

int getFirstVertexOnPath() {
    FOR (i, 0, n) {
        if (Size(t[i]) == 1) {
            return i;
        }
    }

    return -1;
}

int getNext(int v) {
    FOREACH (w, t[v]) {
        if (!vis[w]) {
            return w;
        }
    }

    return -1;
}

void preparePath() {
    int idx = 0;
    for (int v = getFirstVertexOnPath(); v != -1; v = getNext(v)) {
        path[idx++] = v;
        vis[v] = 1;
    }
}

int countDifferentParts(char *colors) {
    int cnt = 1;
    FOR (i, 1, n) {
        if (colors[path[i]] != colors[path[i - 1]]) {
            ++cnt;
        }
    }

    return cnt;
}

bool solvePath() {
    if (n == 1) {
        return inputColors[0] == outputColors[0];
    }

    preparePath();

    int cntInput = countDifferentParts(inputColors);
    int cntOutput = countDifferentParts(outputColors);

    int mxAllowedOutputParts = cntInput - (inputColors[path[0]] != outputColors[path[0]] ? 1 : 0) - (inputColors[path[n - 1]] != outputColors[path[n - 1]] ? 1 : 0);

    return cntOutput <= mxAllowedOutputParts;
}

bool noChanges() {
    FOR (i, 0, n) {
        if (inputColors[i] != outputColors[i]) {
            return false;
        }
    }

    return true;
}

bool containsNeighborsOfTheSameColor() {
    FOR (v, 0, n) {
        int color = outputColors[v];
        FOREACH (w, t[v]) {
            if (outputColors[w] == color) {
                return true;
            }
        }
    }

    return false;
}

bool solve() {
    if (noChanges()) {
        return true;
    }

    int inputColorsHash = getColorsHash(inputColors);
    int outputColorsHash = getColorsHash(outputColors);

    if (inputColorsHash < 3) {
        debug("one color in input", inputColorsHash - 1);
        debug("output colors hash", outputColorsHash);
        return inputColorsHash == outputColorsHash;
    } else if (outputColorsHash < 3) {
        debug("multiple colors in input but one color in output", outputColorsHash - 1);
        return true;
    } else if (containsThreeDegVertex()) {
        debug("tree contains three deg vertex", -1);
        return containsNeighborsOfTheSameColor();
    } else {
        debug("tree is a path", -1);
        return solvePath();
    }
}

void rob(int test) {
    cleanup();
    read();
    bool result = solve();

    if (result) {
        printf("TAK\n");
    } else {
        printf("NIE\n");
    }
}

int main() {
    int test = 1;
    scanf("%d", &test);

    FORE (i, 1, test) rob(i);

    return 0;
}