#include<bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 7; string p, k; vector<int>ed[N]; bool Line(int n) { int il1 = 0, il2 = 0, s1 = 0, s2 = 0; for(int i = 1; i <= n; ++i) { if(ed[i].size() == 1) { s1 = p[i] - '0'; s2 = k[i] - '0'; if(p[ed[i][0]] != p[i]) ++il1; if(k[ed[i][0]] != k[i]) ++il2; }else { if(p[ed[i][0]] != p[i]) ++il1; if(k[ed[i][0]] != k[i]) ++il2; if(p[ed[i][1]] != p[i]) ++il1; if(k[ed[i][1]] != k[i]) ++il2; } } il1 = il1 / 2 + 1; il2 = il2 / 2 + 1; //cout << il1 << " " << il2 << "\n"; if(il2 > il1) return false; if(il1 > il2) return true; if(s1 != s2) return false; return true; } void Solve() { int n, a, b, i1 = 0, i2 = 0, m = 0; cin >> n; cin >> p >> k; p = '#' + p; k = '#' + k; for(int i = 1; i <= n; ++i) ed[i].clear(); for(int i = 1; i < n; ++i) { cin >> a >> b; ed[a].push_back(b); ed[b].push_back(a); } if(p == k) { cout << "TAK\n"; return; } for(int i = 1; i <= n; ++i) { m = max(m, (int)ed[i].size()); if(p[i] == '1') ++i1; if(k[i] == '1') ++i2; } if((i1 == 0 && i2 > 0) || (i2 < n && i1 == n)) { cout << "NIE\n"; return; } if(m < 3) { if(Line(n)) cout << "TAK\n"; else cout << "NIE\n"; return; } for(int i = 1; i <= n; ++i) { int il = 0; for(int j = 0; j < (int)ed[i].size(); ++j) if(k[ed[i][j]] == k[i]) ++il; if((il >= 1 && il < (int)ed[i].size()) || il >= 2) { cout << "TAK\n"; return; } } cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) Solve(); return 0; }
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 | #include<bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 7; string p, k; vector<int>ed[N]; bool Line(int n) { int il1 = 0, il2 = 0, s1 = 0, s2 = 0; for(int i = 1; i <= n; ++i) { if(ed[i].size() == 1) { s1 = p[i] - '0'; s2 = k[i] - '0'; if(p[ed[i][0]] != p[i]) ++il1; if(k[ed[i][0]] != k[i]) ++il2; }else { if(p[ed[i][0]] != p[i]) ++il1; if(k[ed[i][0]] != k[i]) ++il2; if(p[ed[i][1]] != p[i]) ++il1; if(k[ed[i][1]] != k[i]) ++il2; } } il1 = il1 / 2 + 1; il2 = il2 / 2 + 1; //cout << il1 << " " << il2 << "\n"; if(il2 > il1) return false; if(il1 > il2) return true; if(s1 != s2) return false; return true; } void Solve() { int n, a, b, i1 = 0, i2 = 0, m = 0; cin >> n; cin >> p >> k; p = '#' + p; k = '#' + k; for(int i = 1; i <= n; ++i) ed[i].clear(); for(int i = 1; i < n; ++i) { cin >> a >> b; ed[a].push_back(b); ed[b].push_back(a); } if(p == k) { cout << "TAK\n"; return; } for(int i = 1; i <= n; ++i) { m = max(m, (int)ed[i].size()); if(p[i] == '1') ++i1; if(k[i] == '1') ++i2; } if((i1 == 0 && i2 > 0) || (i2 < n && i1 == n)) { cout << "NIE\n"; return; } if(m < 3) { if(Line(n)) cout << "TAK\n"; else cout << "NIE\n"; return; } for(int i = 1; i <= n; ++i) { int il = 0; for(int j = 0; j < (int)ed[i].size(); ++j) if(k[ed[i][j]] == k[i]) ++il; if((il >= 1 && il < (int)ed[i].size()) || il >= 2) { cout << "TAK\n"; return; } } cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) Solve(); return 0; } |