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
#include <bits/stdc++.h>

using namespace std;

int t, n, maxDeg = 0;
vector<int> tree[100002];
string currentColors;
string desiredColors;

void solve() {
    if (currentColors == desiredColors) { // no changes - no problem!
        cout << "TAK\n";
        return;
    }
    if (n == 1) { // changes within one node tree - problem!
        cout << "NIE\n";
        return;
    }
    if (maxDeg <= 2) { // tree-line with changes
        int leaf = 0;
        for (int i = 1; i <= n; ++i) {
            if (tree[i].size() == 1) {
                leaf = i;
                break;
            }
        }
        int currentNumOfColors = 0, desiredNumOfColors = 0;
        int currentColor = currentColors[leaf - 1];
        int desiredColor = desiredColors[leaf - 1];
        int currentInitColor = currentColor;
        int desiredInitColor = desiredColor;
        int prev = leaf, curr = tree[leaf][0];
        while (true) {
            if (currentColor != currentColors[curr - 1]) {
                currentNumOfColors++;
                currentColor = currentColors[curr - 1];
            }
            if (desiredColor != desiredColors[curr - 1]) {
                desiredNumOfColors++;
                desiredColor = desiredColors[curr - 1];
            }
            if (tree[curr].size() == 1) {
                currentNumOfColors++;
                desiredNumOfColors++;
                break;
            } else {
                if (tree[curr][0] != prev) {
                    prev = curr;
                    curr = tree[prev][0];
                } else {
                    prev = curr;
                    curr = tree[prev][1];
                }
            }
        }
        if (currentNumOfColors > desiredNumOfColors) {
            cout << "TAK\n";
            return;
        } else if (currentNumOfColors == desiredNumOfColors && currentInitColor == desiredInitColor) {
            cout << "TAK\n";
            return;
        } else {
            cout << "NIE\n";
            return;
        }
    }
    int numOfColors = 0;
    for (int i = 0; i < n; ++i) {
        numOfColors += (currentColors[i] == '0');
    }
    if (numOfColors == 0 || numOfColors == n) {
        cout << "NIE\n"; 
    }
    for (int i = 1; i <= n; ++i) {
        if (tree[i].size() >= 3) {
            for (int j = 0; j < tree[i].size(); ++j) {
                if (desiredColors[i - 1] == desiredColors[tree[i][j] - 1]) {
                    cout << "TAK\n";
                    return;
                }
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (currentColors[i - 1] != desiredColors[i - 1]) {
            continue;
        }
        for (int j = 0; j < tree[i].size(); ++j) {
            if (currentColors[tree[i][j] - 1] == desiredColors[tree[i][j] - 1]) {
                cout << "TAK\n";
                return;
            }
        }
    }
    cout << "NIE\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> t;
    while (t--) {
        cin >> n;
        cin >> currentColors;
        cin >> desiredColors;
        int a, b;
        maxDeg = 0;
        for (int i = 1; i < n; ++i) {
            cin >> a >> b;
            tree[a].push_back(b);
            tree[b].push_back(a);
            maxDeg = max(maxDeg, int(tree[a].size()));
            maxDeg = max(maxDeg, int(tree[b].size()));
        }

        solve();

        for (int i = 1; i <= n; ++i) {
            tree[i].clear();
        }
        maxDeg = 0;
    }

    return 0;
}