#include<bits/stdc++.h> //#define int unsigned short int //#define int long long #define ll long long #include <time.h> #include <chrono> #include <iomanip> #define BE(x) x.begin(),x.end() #define EB(x) x.end(),x.begin() #define st first #define nd second using namespace std; using namespace std::chrono; vector<int> algwyndlg; /* #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") //#pragma GCC target("avx,avx2,fma") #pragma unroll */ int main(){ // ios::sync_with_stdio(false); cin.tie(NULL); int q;cin>>q;while(q--){ int n;cin>>n; string now,then; cin>>now>>then; vector<vector<int>> G(n); for(int i=0;i<n-1;i++){ int k1,k2;cin>>k1>>k2;k1--;k2--; G[k1].push_back(k2); G[k2].push_back(k1); } int l1=0; for(auto&x:G)if(x.size()==1)l1++; // cout<<"l1:"<<l1<<endl; vector<bool> nowB(2), thenB(2); for(char x:now) nowB[x-'0']=true; for(char x:then) thenB[x-'0']=true; auto doIt = [&](){ if(now==then) return"TAK"; if(!thenB[0]) return nowB[1]?"TAK":"NIE"; if(!thenB[1]) return nowB[0]?"TAK":"NIE"; if(!nowB[0]) return thenB[0]?"NIE":"TAK"; if(!nowB[1]) return thenB[1]?"NIE":"TAK"; if(l1!=2) {for(int i=0;i<n;i++) for(auto x:G[i]) if(then[i]==then[x]) return "TAK"; return "NIE";} int in=0; while(G[in].size()!=1) in++; int last=-1; int nowD=0,thenD=0; if(now[in]!=then[in]) thenD++; while(G[in].size()>1||last==-1){ if(last==G[in][0]) {last=in; in=G[in][1];} else {last=in; in=G[in][0];} if(now[in]!=now[last]) nowD++; if(then[in]!=then[last]) thenD++; } // cout<<nowD<<" "<<thenD<<endl; if(nowD>=thenD) return "TAK"; return "NIE"; }; cout<<doIt()<<endl; } }
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 | #include<bits/stdc++.h> //#define int unsigned short int //#define int long long #define ll long long #include <time.h> #include <chrono> #include <iomanip> #define BE(x) x.begin(),x.end() #define EB(x) x.end(),x.begin() #define st first #define nd second using namespace std; using namespace std::chrono; vector<int> algwyndlg; /* #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") //#pragma GCC target("avx,avx2,fma") #pragma unroll */ int main(){ // ios::sync_with_stdio(false); cin.tie(NULL); int q;cin>>q;while(q--){ int n;cin>>n; string now,then; cin>>now>>then; vector<vector<int>> G(n); for(int i=0;i<n-1;i++){ int k1,k2;cin>>k1>>k2;k1--;k2--; G[k1].push_back(k2); G[k2].push_back(k1); } int l1=0; for(auto&x:G)if(x.size()==1)l1++; // cout<<"l1:"<<l1<<endl; vector<bool> nowB(2), thenB(2); for(char x:now) nowB[x-'0']=true; for(char x:then) thenB[x-'0']=true; auto doIt = [&](){ if(now==then) return"TAK"; if(!thenB[0]) return nowB[1]?"TAK":"NIE"; if(!thenB[1]) return nowB[0]?"TAK":"NIE"; if(!nowB[0]) return thenB[0]?"NIE":"TAK"; if(!nowB[1]) return thenB[1]?"NIE":"TAK"; if(l1!=2) {for(int i=0;i<n;i++) for(auto x:G[i]) if(then[i]==then[x]) return "TAK"; return "NIE";} int in=0; while(G[in].size()!=1) in++; int last=-1; int nowD=0,thenD=0; if(now[in]!=then[in]) thenD++; while(G[in].size()>1||last==-1){ if(last==G[in][0]) {last=in; in=G[in][1];} else {last=in; in=G[in][0];} if(now[in]!=now[last]) nowD++; if(then[in]!=then[last]) thenD++; } // cout<<nowD<<" "<<thenD<<endl; if(nowD>=thenD) return "TAK"; return "NIE"; }; cout<<doIt()<<endl; } } |