#include <bits/stdc++.h> using namespace std; struct node { bool s=0; bool k=0; int stop=0; vector<int> edges; void clean() { stop=0; edges.clear(); } }; vector<node> graf(100001); int maxi=0; void hs(int ind,int last, int zm) { if (graf[last].s != graf[ind].s)zm++; maxi = max(maxi,zm); for (int i=0;i<graf[ind].edges.size();++i) { hs(graf[ind].edges[i] , ind ,zm); } return; } void hk(int ind,int last, int zm) { if (graf[last].k != graf[ind].k)zm++; maxi = max(maxi,zm); for (int i=0;i<graf[ind].edges.size();++i) { hk(graf[ind].edges[i] , ind ,zm); } return; } int main() { int t; cin >> t; for(int z =0;z<t;++z) { int n; cin >> n; for (int i=0;i<n;++i)graf[i].clean(); string s; cin >> s; for (int i=0;i<n;++i) { graf[i+1].s = (s[i] == '1' ? 1 : 0); } string s2; cin >> s2; for (int i=0;i<n;++i) { graf[i+1].k = (s2[i] == '1' ? 1 : 0); } for (int i=0;i<n-1;++i) { int pom1; int pom2; cin >> pom1 >> pom2; graf[pom1].edges.push_back(pom2); graf[pom1].stop+=(graf[pom2].k == 0 ? -1 : 1 ); graf[pom2].edges.push_back(pom1); graf[pom2].stop+=(graf[pom1].k == 0 ? -1 : 1 ); } if (s == s2) { cout << "TAK\n"; continue; } bool bryk =0; for(int i=0;i<n-1;++i) { if (graf[i].edges.size() <= 2)continue; if (graf[i].k == 1) { if (!(graf[i].stop == -graf[i].edges.size()) ) { bryk = 1; break; } } if (graf[i].k == 0) { if (!(graf[i].stop == graf[i].edges.size()) ) { bryk = 1; break; } } } if (bryk) { cout << "TAK"<<endl; continue; } maxi =0; hs(0,0,0); int o1 = maxi; maxi =0; hk(0,0,0); int o2 = maxi; if (o1 > o2) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } /* 3 4 1011 1100 1 2 2 3 2 4 2 10 10 1 2 2 10 01 1 2 1 5 01110 10001 1 2 1 3 1 4 2 5 */
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 | #include <bits/stdc++.h> using namespace std; struct node { bool s=0; bool k=0; int stop=0; vector<int> edges; void clean() { stop=0; edges.clear(); } }; vector<node> graf(100001); int maxi=0; void hs(int ind,int last, int zm) { if (graf[last].s != graf[ind].s)zm++; maxi = max(maxi,zm); for (int i=0;i<graf[ind].edges.size();++i) { hs(graf[ind].edges[i] , ind ,zm); } return; } void hk(int ind,int last, int zm) { if (graf[last].k != graf[ind].k)zm++; maxi = max(maxi,zm); for (int i=0;i<graf[ind].edges.size();++i) { hk(graf[ind].edges[i] , ind ,zm); } return; } int main() { int t; cin >> t; for(int z =0;z<t;++z) { int n; cin >> n; for (int i=0;i<n;++i)graf[i].clean(); string s; cin >> s; for (int i=0;i<n;++i) { graf[i+1].s = (s[i] == '1' ? 1 : 0); } string s2; cin >> s2; for (int i=0;i<n;++i) { graf[i+1].k = (s2[i] == '1' ? 1 : 0); } for (int i=0;i<n-1;++i) { int pom1; int pom2; cin >> pom1 >> pom2; graf[pom1].edges.push_back(pom2); graf[pom1].stop+=(graf[pom2].k == 0 ? -1 : 1 ); graf[pom2].edges.push_back(pom1); graf[pom2].stop+=(graf[pom1].k == 0 ? -1 : 1 ); } if (s == s2) { cout << "TAK\n"; continue; } bool bryk =0; for(int i=0;i<n-1;++i) { if (graf[i].edges.size() <= 2)continue; if (graf[i].k == 1) { if (!(graf[i].stop == -graf[i].edges.size()) ) { bryk = 1; break; } } if (graf[i].k == 0) { if (!(graf[i].stop == graf[i].edges.size()) ) { bryk = 1; break; } } } if (bryk) { cout << "TAK"<<endl; continue; } maxi =0; hs(0,0,0); int o1 = maxi; maxi =0; hk(0,0,0); int o2 = maxi; if (o1 > o2) { cout << "TAK\n"; } else { cout << "NIE\n"; } } } /* 3 4 1011 1100 1 2 2 3 2 4 2 10 10 1 2 2 10 01 1 2 1 5 01110 10001 1 2 1 3 1 4 2 5 */ |