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

using namespace std;

static vector<int> graf[100000];

static char tab[100001];
static char tab2[100001];

int main(){
        int t;
        scanf("%i", &t);

        while(t--){
                int n;
                scanf("%i", &n);

                scanf(" %s", tab);
                scanf(" %s", tab2);

                for(int i = 0; i < n-1; ++i){
                        int a, b;
                        scanf("%i %i", &a, &b);
                        --a;
                        --b;

                        graf[a].push_back(b);
                        graf[b].push_back(a);
                }

                int suma1 = 0;
                int suma2 = 0;

                bool potr = false;
                int poj = -1;
                bool powt = false;
                bool iden = true;

                for(int i = 0; i < n; ++i){
                        suma1 += tab[i] - '0';
                        suma2 += tab2[i] - '0';

                        if(tab[i] != tab2[i])
                                iden = false;

                        if(graf[i].size() > 2)
                                potr = true;
                        else if(graf[i].size()==1)
                                poj = i;
                        for(unsigned int j = 0; j < graf[i].size(); ++j){
                                int w2 = graf[i][j];
                                if(tab2[i] == tab2[w2])
                                        powt = true;
                        }
                }

                if(suma1 == n || !suma1){
                        if(suma1 == suma2){
                                puts("TAK");
                        }
                        else{
                                puts("NIE");
                        }

                        for(int i = 0; i < n; ++i)
                                graf[i].clear();

                        continue;
}

                if(!powt){
                        if(iden)
                                puts("TAK");
                        else
                                puts("NIE");

                        for(int i = 0; i < n; ++i)
                                graf[i].clear();

                        continue;
                }

                int curr = poj;
                int pop = -1;
                int popc = -1;
                int popc2 = -1;
                int licznik = 0;
                int licznik2 = 0;
                if(!potr){
                        while(true){
                                if(tab[curr] != popc)
                                        ++licznik;
                                if(tab2[curr] != popc2)
                                        ++licznik2;

                                int ncurr;
                                if(graf[curr][0] != pop)
                                        ncurr = graf[curr][0];
                                else if(graf[curr].size() > 1)
                                        ncurr = graf[curr][1];
                                else
                                        break;

                                pop = curr;
                                popc = tab[curr];
popc2 = tab2[curr];
                                curr = ncurr;
                        }

                        if(licznik == licznik2 && tab[poj] == tab2[poj])
                                puts("TAK");
                        else if(licznik>licznik2)
                                puts("TAK");
                        else
                                puts("NIE");

                        for(int i = 0; i < n; ++i)
                                graf[i].clear();

                        continue;
                }

                puts("TAK");

                for(int i = 0; i < n; ++i)
                        graf[i].clear();
        }
        return 0;
}