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

using namespace std;

class Solution {
    int n;
    vector<bool> values;
    vector<bool> desired;
    vector<vector<int> > adj;

    int root;

    bool calculate_straight() {
        int end = -1;
        for(int i = 0; i < n; ++i) {
            if(adj[i].size() == 1) {
                end = i;
                break;
            }
        }
        vector<bool> order;
        order.push_back(values[end]);
        int prev = -1;
        int cur = end;
        while(adj[cur].size() > 1) {
            int old_prev = prev;
            prev = cur;
            if(old_prev == adj[cur][0]) {
                cur = adj[cur][1];
            }else {
                cur = adj[cur][0];
            }
            if(values[cur] != order.back()) {
                order.push_back(values[cur]);
            }
        }

        vector<bool> desired_order;
        desired_order.push_back(desired[end]);
        prev = -1;
        cur = end;
        while(adj[cur].size() > 1) {
            int old_prev = prev;
            prev = cur;
            if(old_prev == adj[cur][0]) {
                cur = adj[cur][1];
            }else {
                cur = adj[cur][0];
            }
            if(desired[cur] != desired_order.back()) {
                desired_order.push_back(desired[cur]);
            }
        }
        
        // compare orders
        int a = 0, b = 0;
        while(a < order.size() && b < desired_order.size()) {
            if(order[a] != desired_order[b]) ++a;
            else {
                ++a; ++b;
            }
        }

        if(b == desired_order.size()) {
            return true;
        }else {
            return false;
        }
    }

    
public:
    void input() {
        cin>>n;
        values.resize(n);
        desired.resize(n);
        for(int i = 0; i < n; ++i) {
            char c; cin>>c;
            values[i] = c == '1';
        }
        for(int i = 0; i < n; ++i) {
            char c; cin>>c;
            desired[i] = c == '1';
        }
        adj.resize(n);
        for(int i = 0; i < n - 1; ++i) {
            int a, b; cin>>a>>b; --a; --b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }
    bool calculate() {
        // find root with adj.size() > 2
        root = -1;
        for(int i = 0; i < n; ++i) {
            if(adj[i].size() > 2) {
                root = i;
                break;
            }
        }
        if(root == -1) { // the input is a straight line
            return calculate_straight();
        }
        // otherwise calculate degrees and proceed
        vector<int> deg(n);
        for(int i = 0; i < n; ++i) {
            if(i == root) {
                deg[i] = adj[i].size();
            }else {
                deg[i] = adj[i].size() - 1;
            }
        }
        int desired_0 = 0;
        int desired_1 = 0;
        for(bool d : desired) {
            if(d == 0) ++desired_0;
            else ++desired_1;
        }
        queue<int> leaves;
        vector<int> possible(n, -1); // possible values to be taken from this subtree (0 - 0, 1 - 1, 2 - 1 xor 0, -1 - no, 3 - 1 or 0)
        for(int i = 0; i < n; ++i) {
            if(deg[i] == 0) {leaves.push(i); possible[i] = values[i]; }
        }
        while(!leaves.empty()) {
            int v = leaves.front(); leaves.pop();
            if(possible[v] < 2 && possible[v] != values[v]) {
                possible[v] = 2;
            }

            int parent = -1;
            for(int to : adj[v]) {
                if(deg[to] > 0) {
                    parent = to;
                    break;
                }
            }

            if(parent == -1) { // this is the root (has at least 3 children)
                if(possible[v] == 3 || possible[v] == 2) {
                    return true;
                }else {
                    if(possible[v] == 0 && desired_1 == 0) {
                        return true;
                    }else if(possible[v] == 1 && desired_0 == 0) {
                        return true;
                    }else {
                        return false;
                    }
                }
            }

            if(possible[parent] == -1) {
                possible[parent] = possible[v];
            }else if(possible[parent] != possible[v]) {
                possible[parent] = 3;
            }

            --deg[parent];
            if(deg[parent] == 0) {
                leaves.push(parent);
            }
        }
        return false; // this doesn't happen
    }
};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t;
    cin>>t;
    vector<bool> res(t);
    for(int i = 0; i < t; ++i) {
        Solution s;
        s.input();
        res[i] = s.calculate();
    }

    for(int i = 0; i < t; ++i) {
        if(res[i]) {
            cout<<"TAK"<<endl;
        }else {
            cout<<"NIE"<<endl;
        }
    }
}