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
#include <iostream>
#include <vector>
#include <string>

struct Intervals {
    int red = 0;
    int black = 0;
};

Intervals get_interval(const std::string &state, const std::vector<int> &indices) {
    Intervals result;
    result.red = 0;
    result.black = 0;

    for (int i = 1; i < indices.size(); ++i) {
        if (state[indices[i]] != state[indices[i - 1]]) {
            if (state[indices[i - 1]] == '0') {
                ++result.red;
            }
            else {
                ++result.black;
            }
        }
    }

    if (state[indices[indices.size() - 1]] == '0') {
        ++result.red;
    }
    else {
        ++result.black;
    }

    return result;
}

bool solution() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> graph(n, std::vector<int>());

    std::string original;
    std::cin >> original;

    std::string expected;
    std::cin >> expected;

    int x, y;
    for (int i = 0; i < n - 1; ++i) {
        std::cin >> x >> y;
        graph[x - 1].push_back(y - 1);
        graph[y - 1].push_back(x - 1);
    }

    bool is_red_orig = false;
    bool is_red_exp = false;
    bool is_black_orig = false;
    bool is_black_exp = false;

    for (int i = 0; i < n; ++i) {
        if (original[i] == '0') {
            is_red_orig = true;
        }
        else {
            is_black_orig = true;
        }
        if (expected[i] == '0') {
            is_red_exp = true;
        }
        else {
            is_black_exp = true;
        }
    }

    if (is_black_exp && !is_black_orig) {
        return false;
    }

    if (is_red_exp && !is_red_orig) {
        return false;
    }

    if (is_black_exp ^ is_red_exp) {
        return true;
    }

    // n >= 2 here

    for (int i = 0; i < n; ++i) {
        if (graph[i].size() > 2) {
            return true;
        }
    }

    // wszystkie wierzchołki mają co najwyżej dwóch sąsiadów -- lista

    int leaf;
    for (int i = 0; i < n; ++i) {
        if (graph[i].size() == 1) {
            leaf = i;
            break;
        }
    }

    std::vector<int> indices(n);
    indices[0] = leaf;
    indices[1] = graph[leaf][0];
    for (int i = 2; i < n; ++i) {
        if (graph[indices[i - 1]][0] == indices[i - 2]) {
            indices[i] = graph[indices[i - 1]][1];
        }
        else {
            indices[i] = graph[indices[i - 1]][0];
        }
    }

    const Intervals orig_intervals = get_interval(original, indices);
    const Intervals exp_intervals = get_interval(expected, indices);

    if (orig_intervals.red < exp_intervals.red) {
        return false;
    }
    else if (orig_intervals.black < exp_intervals.black) {
        return false;
    }
    else if (orig_intervals.red == exp_intervals.red && orig_intervals.black == exp_intervals.black) {
        return original[indices[0]] == expected[indices[0]];
    }
    else {
        return true;
    }
}

int main() {
    int t;
    std::cin >> t;
    while (t--) {
        if (solution()) {
            std::cout << "TAK\n";
        }
        else {
            std::cout << "NIE\n";
        }
    }
    return 0;
}