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

using namespace std;

int main() {
    int t, n, a, b;
    cin >> t;
    for (int ti = 0; ti < t; ++ti) {
        cin >> n;

        bool starting[n];
        bool expected[n];
        vector<int> edges[n];
        int rank[n];
        string s;
        cin.ignore(256, '\n');
        bool s_all1 = true;
        bool s_all0 = true;
        bool e_all1 = true;
        bool e_all0 = true;

        getline(cin, s);
        for (int i = 0; i < n; ++i) {
            rank[i] = 0;
            if(s[i] == '1') {
                s_all0 = false;
                starting[i] = true;
            } else {
                s_all1 = false;
                starting[i] = false;
            }
        }
        getline(cin, s);
        for (int i = 0; i < n; ++i) {
            if(s[i] == '1') {
                e_all0 = false;
                expected[i] = true;
            } else {
                e_all1 = false;
                expected[i] = false;
            }
        }

        bool has_samecol = false;
        bool already_done = true;

        for (int i = 0; i < n; ++i) {
            if (starting[i] != expected[i]) {
                already_done = false;
            }
        }

        for (int i = 0; i < n-1; ++i) {
            cin >> a >> b;
            edges[a-1].push_back(b-1);
            edges[b-1].push_back(a-1);
            rank[a-1]++;
            rank[b-1]++;
            if(expected[a-1] == expected[b-1]) {
                has_samecol = true;
            }
        }

        if (s_all0 && e_all0) {
            // cerr << "ALREADY DONE (MONO)" << endl;
            cout << "TAK" << endl;
            continue;
        } else if (s_all1 && e_all1) {
            // cerr << "ALREADY DONE (MONO)" << endl;
            cout << "TAK" << endl;
            continue;
        } else if (s_all0 && !e_all0) {
            // cerr << "REQUIRES FULL TRANSFORM" << endl;
            cout << "NIE" << endl;
            continue;
        } else if (s_all1 && !e_all1) {
            // cerr << "REQUIRES FULL TRANSFORM" << endl;
            cout << "NIE" << endl;
            continue;
        }

        if (already_done) {
            // cerr << "ALREADY DONE" << endl;
            cout << "TAK" << endl;
            continue;
        }

        if (!has_samecol) {
            // cerr << "HAS NO SAMECOL" << endl;
            cout << "NIE" << endl;
            continue;
        }

        int max_rank = 0;
        int some_rank1 = -1;

        for (int i = 0; i < n; ++i) {
            if (edges[i].size() > max_rank) {
                max_rank = (int)edges[i].size();
            }
            if (edges[i].size() == 1) {
                some_rank1 = i;
            }
        }



        if (max_rank > 2) {
            // cerr << "HAS RANK > 2" << endl;
            cout << "TAK" << endl;
            continue;
        }

        // now we have a path, with all vertices of rank 2 and 2 leaves

        int last_idx = some_rank1;
        int cur_idx = some_rank1;
        int start_segments = 1;
        int expected_segments = 1;
        bool starting_first_segment = starting[last_idx];
        bool expected_first_segment = expected[last_idx];

        for (int i = 0; i < n; ++i) {
            // cerr << "NOW ON EDGE " << last_idx+1 << " TO " << cur_idx+1 << endl;
            if (starting[cur_idx] != starting[last_idx]) {
                // cerr << "STARTING SEGMENT SWITCH" << endl;
                start_segments++;
            }
            if (expected[cur_idx] != expected[last_idx]) {
                // cerr << "EXPECTED SEGMENT SWITCH" << endl;
                expected_segments++;
            }
            if (edges[cur_idx].size() > 1) {
                int next_idx = edges[cur_idx][0] + edges[cur_idx][1] - last_idx;
                last_idx = cur_idx;
                cur_idx = next_idx;
            } else if (last_idx == cur_idx) {
                cur_idx = edges[last_idx][0];
            }
        }

        if (start_segments > expected_segments) {
            // cerr << "START_SEGMENTS > EXPECTED_SEGMENTS" << endl;
            cout << "TAK" << endl;
        } else if (start_segments < expected_segments) {
            // cerr << "START_SEGMENTS < EXPECTED_SEGMENTS" << endl;
            cout << "NIE" << endl;
        } else if (starting_first_segment == expected_first_segment) {
            // cerr << "START_FIRST_SEGMENT == EXPECTED_FIRST_SEGMENT" << endl;
            cout << "TAK" << endl;
        } else {
            // cerr << "START_FIRST_SEGMENT != EXPECTED_FIRST_SEGMENT" << endl;
            cout << "NIE" << endl;
        }

//        // DEBUG
//        for (int i = 0; i < n; ++i) {
//            cerr << starting[i] << ": " << expected[i] << endl;
//        }
//        for (int i = 0; i < n; ++i) {
//            cerr << "NBS OF " << i << endl;
//            for (int j = 0; j < edges[i].size(); ++j){
//                cerr << edges[i][j] << endl;
//            }
//        }
    }
    return 0;
}