#include <bits/stdc++.h> using namespace std; constexpr int maxN = 1e5+7; int T,n,ile[maxN]; string s1,s2,ans1,ans2; bool ans,odw[maxN],sciezka=true; vector<int>v[maxN]; void clear() { for(int i=1;i<=n;i++) { v[i].clear(); v[i].shrink_to_fit(); ile[i] = 0; odw[i] = false; sciezka = true; } } void dfs(int x) { odw[x] = true; for(int y:v[x]) { if(!odw[y]) { if(s2[x] == s2[y]) ans = true; dfs(y); } } } void dfs1(int x, int oj, char last) { for(int y:v[x]) if(y != oj) { if(s1[y] != last) ans1.push_back(s1[y]); dfs1(y,x,s1[y]); } } void dfs2(int x, int oj, char last) { for(int y:v[x]) if(y != oj) { if(s2[y] != last) ans2.push_back(s2[y]); dfs2(y,x,s2[y]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>T; while(T--) { cin>>n; cin>>s1; cin>>s2; s1 = '!'+s1; s2 = '!'+s2; for(int i=1;i<n;i++) { int a,b; cin>>a>>b; v[a].push_back(b); v[b].push_back(a); ile[a]++; ile[b]++; if(ile[a] > 2 || ile[b] > 2) sciezka = false; } if(s1 == s2) { cout<<"TAK"<<endl; clear(); continue; } if(n<=1){ if(s1==s2) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; clear(); continue; } if(sciezka) { ans1.clear(); ans2.clear(); for(int i=1;i<=n;i++) if(ile[i] == 1) { ans1.push_back(s1[i]); ans2.push_back(s2[i]); dfs1(i,0,s1[i]); dfs2(i,0,s2[i]); break; } if(ans1.size() > ans2.size()) cout<<"TAK"<<endl; else { if(ans2.size() > ans1.size() || ans1[0] != ans2[0]) cout<<"NIE"<<endl; else cout<<"TAK"<<endl; } clear(); continue; } int zera1=0,zera2=0; for(int i=1;i<=n;i++) { zera1 += (s1[i] == '0'); zera2 += (s2[i] == '0'); } int jedynki1 = n-zera1, jedynki2 = n-zera2; ans = true; if(zera1==n && jedynki2) ans = false; if(jedynki1==n && zera2) ans = false; if(!ans) { cout<<"NIE"<<endl; clear(); continue; } ans = false; dfs(1); if(ans) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; clear(); } }
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 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 1e5+7; int T,n,ile[maxN]; string s1,s2,ans1,ans2; bool ans,odw[maxN],sciezka=true; vector<int>v[maxN]; void clear() { for(int i=1;i<=n;i++) { v[i].clear(); v[i].shrink_to_fit(); ile[i] = 0; odw[i] = false; sciezka = true; } } void dfs(int x) { odw[x] = true; for(int y:v[x]) { if(!odw[y]) { if(s2[x] == s2[y]) ans = true; dfs(y); } } } void dfs1(int x, int oj, char last) { for(int y:v[x]) if(y != oj) { if(s1[y] != last) ans1.push_back(s1[y]); dfs1(y,x,s1[y]); } } void dfs2(int x, int oj, char last) { for(int y:v[x]) if(y != oj) { if(s2[y] != last) ans2.push_back(s2[y]); dfs2(y,x,s2[y]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>T; while(T--) { cin>>n; cin>>s1; cin>>s2; s1 = '!'+s1; s2 = '!'+s2; for(int i=1;i<n;i++) { int a,b; cin>>a>>b; v[a].push_back(b); v[b].push_back(a); ile[a]++; ile[b]++; if(ile[a] > 2 || ile[b] > 2) sciezka = false; } if(s1 == s2) { cout<<"TAK"<<endl; clear(); continue; } if(n<=1){ if(s1==s2) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; clear(); continue; } if(sciezka) { ans1.clear(); ans2.clear(); for(int i=1;i<=n;i++) if(ile[i] == 1) { ans1.push_back(s1[i]); ans2.push_back(s2[i]); dfs1(i,0,s1[i]); dfs2(i,0,s2[i]); break; } if(ans1.size() > ans2.size()) cout<<"TAK"<<endl; else { if(ans2.size() > ans1.size() || ans1[0] != ans2[0]) cout<<"NIE"<<endl; else cout<<"TAK"<<endl; } clear(); continue; } int zera1=0,zera2=0; for(int i=1;i<=n;i++) { zera1 += (s1[i] == '0'); zera2 += (s2[i] == '0'); } int jedynki1 = n-zera1, jedynki2 = n-zera2; ans = true; if(zera1==n && jedynki2) ans = false; if(jedynki1==n && zera2) ans = false; if(!ans) { cout<<"NIE"<<endl; clear(); continue; } ans = false; dfs(1); if(ans) cout<<"TAK"<<endl; else cout<<"NIE"<<endl; clear(); } } |