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

using namespace std;
#define LL long long
#define BIGMOD 1000000007LL

#define DBG(X)

void pushBackIfDifferentThanLast(vector<int> &vec, int elem) {
    if (vec.empty())
    {
        vec.push_back(elem);
        return;
    }
    if (elem != vec[vec.size()-1])
    {
        vec.push_back(elem);
    }
}

void print_vec(string m, vector<int> v)
{
    printf("%s", m.c_str());
    for (int i=0; i < v.size(); i++)
    {
        printf("%d",v[i]);
    }
}

vector<int> stworzLinie(const vector<vector<int> > &adj, int leafIdx, int forbidden)
{
    DBG(printf("Stworz linie from leafIdx:%d, forbidden=%d\n", leafIdx, forbidden));
    int v = leafIdx;
    int lastIdx = v;
    vector<int> res;
    bool running = true;
    while (running)
    {
        if (adj[v].size() > 2) break; // przypadek gdy wycinamy linie
        res.push_back(v);
        running = false;
        // mamy maks 2 sasiadow
        DBG(printf("linia: v=%d, adj[v].size()=%d, adj[v][0]=%d\n",v, adj[v].size(), adj[v][0]));
        
        for (int i=0; i < adj[v].size(); i++)
        {
            int u = adj[v][i];
            if (u == lastIdx || u == forbidden) continue;
            lastIdx = v;
            v = u;
            running = true;
            break;
        }
    }
    return res;
}

bool sprawdzLinie(const vector<int> &linia, const string &current, const string &desired, int leafIdx)
{
    //printf("sprawdz linie\n");
    if (linia.size() == 0) return true;
    int idx = leafIdx;
    vector<int> colorOrdersCurrent;
    vector<int> colorOrdersDesired;
    for (int i=0; i < linia.size(); i++)
    {
        int v = linia[i];
        pushBackIfDifferentThanLast(colorOrdersCurrent, current[v]-'0');
        pushBackIfDifferentThanLast(colorOrdersDesired, desired[v]-'0');
    }
    DBG(print_vec("current:",colorOrdersCurrent)); DBG(printf("\n"));
    DBG(print_vec("desired:", colorOrdersDesired));DBG(printf("\n"));
    if (colorOrdersDesired.size() > colorOrdersCurrent.size()) return false;
    if (colorOrdersDesired.size() < colorOrdersCurrent.size()) return true;
    
    return colorOrdersDesired[0] == colorOrdersCurrent[0];
}


bool solve(const vector<vector<int> > &adj, string &current, const string &desired)
{
    if (current == desired) return true;
    int n = adj.size();
    if (n==1) return current == desired;
    
    int cntColorsCurrent[2] = {0,0};
    for (int i=0; i < current.size(); i++)
    {
        cntColorsCurrent[current[i]-'0']++;
    }
    bool hasBothColorsCurrent = (cntColorsCurrent[0]>0) && (cntColorsCurrent[1]>0);
    if (!hasBothColorsCurrent)
    {
        return current == desired;
    }

    bool has3Degree = false;
    int leafIdx = -1;
    for (int i=0; i < n; i++)
    {
        if (adj[i].size() >= 3) has3Degree = true;
        if (adj[i].size() == 1) leafIdx = i;
    }
   
    if (!has3Degree)
    {
        vector<int> linia = stworzLinie(adj, leafIdx, -1);
        return sprawdzLinie(linia, current, desired, leafIdx);
    }
    for (int i=0; i < n; i++)
    {
        const vector<int> &others = adj[i];
        bool has[2] = {false, false};
        int cntMatchDesired = 0;
        if (others.size() >= 3)
        {
            for (int v : others)
            {
                if (desired[v] == '0') has[0] = true;
                if (desired[v] == '1') has[1] = true;
            }
            if (has[0] && has[1]) return true;
            // hmm... to moze chociaz maja taki sam desired
            if (desired[i] == '0' && has[0]) return true;
            if (desired[i] == '1' && has[1]) return true;
            // tutaj desired tego "w srodku" jest zupelnie innych od desired jego sasiadow
        }
    }
    // zaden wierzcholek nie spelnia czegos takiego, hmm
    // to wybieramy największy wierzchołek, i dzielimy drzewo na kilka części, każdą z nich rozwiązując rekurencyjnie

    for (int i=0; i < n; i++)
    {
        const vector<int> &others = adj[i];
        int center = i;
        if (others.size() >= 3)
        {
            for (int v : others)
            {
                DBG(printf("hard case at v=%d center=%d\n", v, center));
                vector<int> linia = stworzLinie(adj, v, center);
                bool old = current[v];
                current[v] = '0';
                bool res = sprawdzLinie(linia, current, desired, leafIdx);
                current[v] = '1';
                res |= sprawdzLinie(linia, current, desired, leafIdx);
                current[v] = old;
                if (!res) return false;
            }
        }
    }
    return true;
}


char buf[100010];
int main() {
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        scanf("%s", buf);
        string current(buf);
        scanf("%s", buf);
        string desired(buf);
        vector<vector<int> > adj(n);
        //printf("current=%s desired=%s\n", current.c_str(), desired.c_str());
        for (int i=0; i < n - 1; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            --a; --b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        bool sol = solve(adj, current, desired);
        printf(sol ? "TAK\n" : "NIE\n");
    }
}