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

using namespace std;
using namespace chrono;

static unsigned long x = 123456789, y = 362436069, z = 521288629;

unsigned long xorshf96(void) {          //period 2^96-1
    unsigned long t;
    x ^= x << 16;
    x ^= x >> 5;
    x ^= x << 1;

    t = x;
    x = y;
    y = z;
    z = t ^ x ^ y;

    return z;
}

class timer :  high_resolution_clock {
    const time_point start_time;
public:
    timer() : start_time(now()) {}
    rep elapsed_time() const { return duration_cast<milliseconds>(now() - start_time).count(); }
};

class Graph {
public:
    vector<vector<int>> neighbors;
    vector<char> colors;
    vector<char> targetColors;
    int diffNumber;
    int blackCount;
    bool same = false;

    Graph(int n) {
        this->neighbors.resize(n + 1);
    }

    Graph(int n, string colors, string targetColors) {
        this->neighbors.resize(n);
        this->colors.resize(n - 1);
        this->targetColors.resize(n - 1);
        blackCount = 0;

        for (int i = 0; i < n - 1; i++) {
            this->colors[i] = colors[i];

            if (colors[i] == '1') {
                blackCount++;
            }

            this->targetColors[i] = targetColors[i];
        }


        for (int i = 0; i < n - 1; i++) {
            if (colors[i] != targetColors[i]) {
                diffNumber++;
            }
        }
    }

    void setGraphColors(string colors, int blackCount, int diffNumber) {
        this->blackCount = blackCount;
        this->diffNumber = diffNumber;

        for (int i = 0; i < neighbors.size() - 1; i++) {
            this->colors[i] = colors[i];
        }
    }

    void copyColor(int a, int b) {
        if (this->colors[b] != this->colors[a]) { // ensure color change
            this->colors[b] = this->colors[a];

            if (this->colors[b] == '1') {
                blackCount++;
            } else {
                blackCount--;
            }

            if (this->colors[b] == targetColors[b]) {
                diffNumber--;
            } else {
                diffNumber++;
            }
        }
    }

    void copyColorFromRandomNeighbor(int a) {
        copyColor(neighbors[a + 1][xorshf96() % neighbors[a + 1].size()] - 1, a);
    }

    void printColors() {
        for (int i = 0; i < colors.size(); i++) {
            cout << colors[i];
        }
        cout << endl;
    }

    void addEdge(int a, int b) {
        neighbors[a].push_back(b);
        neighbors[b].push_back(a);
    }

    void print() {
        for (int i = 1; i < neighbors.size(); i++) {
            cout << i << ": ";
            for (int j = 0; j < neighbors[i].size(); j++) {
                cout << neighbors[i][j] << " ";
            }
            cout << endl;
        }
    }
};

int main()
{
    int t;
    cin >> t;
    const int T = 2000;
    timer ti; // create a timer object
    for (int b = 0; b < t; b++) {
        int n;
        cin >> n;
        string colors;
        string targetColors;
        cin >> colors >> targetColors;

        Graph graph(n + 1, colors, targetColors);

        bool polar = true;
        int x, y;

        for (int i = 1; i < n; i++) {
            cin >> x >> y;
            graph.addEdge(x, y);
        }

        for (int i = 0; i < colors.size(); i++) {
            if (colors[i] == targetColors[i]) {
                polar = false;
                break;
            }
        }

        if (polar) {
            cout << "NIE" << endl;
            continue;
        }


        int blackCount = graph.blackCount;
        int diffNumber = graph.diffNumber;


        
        int trash = xorshf96();

        while (graph.diffNumber != 0 && ti.elapsed_time() < T) {
            graph.copyColorFromRandomNeighbor(xorshf96() % n);
            if (graph.blackCount == 0 || graph.blackCount == n) { // same color
                graph.setGraphColors(colors, blackCount, diffNumber);
            }
        }

        if (graph.diffNumber == 0) {
            cout << "TAK" << endl;
        } else {
            cout << "NIE" << endl;
        }
    }

    return 0;
}