Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <cstdio> #include <cassert> #include <vector> #include <tuple> #include <unordered_set> #include <queue> #include <chrono> using namespace std; const int maxn = 100005; int n; char ca[maxn]; char cb[maxn]; class node { public: int nr; int edge_n; vector<int> e; }; vector<node> t; void print_tree() { printf("\n********************\n"); for(int i = 1; i <= n; ++i) { printf("(%d ca:%c cb:%c)", t[i].nr, ca[i], cb[i]); for(int j = 0; j < t[i].edge_n; ++j) { printf(" -> %d", t[i].e[j]); } printf("\n"); } // if (r[k] == 0) { // printf("NIE\n"); // } else { // printf("TAK\n"); // } } bool same_colors_in_input() { char c = ca[1]; for(int i = 1; i <= n; ++i) { if (ca[i] != c) { return false; } } return true; } bool input_equal_output() { for(int i = 1; i <= n; ++i) { if (ca[i] != cb[i]) { return false; } } return true; } bool adjacent_nodes_have_different_color_than_me_at_finish(int k) { char mycolor = cb[t[k].nr]; for(int i = 0; i < t[k].edge_n; ++i) { if (cb[t[k].e[i]] == mycolor) { return false; } } return true; } bool exists_node_with_morethan2_edges_with_diff_adj_colours() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { if (!adjacent_nodes_have_different_color_than_me_at_finish(i)) { return true; } } } return false; } bool exists_node_with_morethan2_edges() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { return true; } } return false; } int calc_changes(int k, int prev) { int res = 0; char lastc; lastc = cb[k]; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; if (cb[k] != lastc) { res++; } lastc = cb[k]; } return res; } int calc_len(int k, int prev) { int res = 0; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; res++; } return res; } // if adj of k have different colours return true // // exists adj of k (called j) that have another adj (!= k) having same colur as j bool check_node(int k) { char c = cb[t[k].e[0]]; for(int i = 0; i < t[k].edge_n; ++i) { if (cb[t[k].e[i]] != c) { return false; } } // all adj have same colours for(int i = 0; i < t[k].edge_n; ++i) { int j = t[k].e[i]; for(int l = 0; l < t[j].edge_n; ++l) { int m = t[j].e[l]; if (m != k) { if (cb[m] == cb[j]) { return true; } for(int i2 = 0; i2 < t[m].edge_n; ++i2) { int j2 = t[m].e[i2]; if (j2 != m) { if (cb[j2] == cb[m]) { return true; } } } if (calc_changes(m, j) < calc_len(m, j)) { return true; } } } } return false; } bool check_nodes_with_morethan2_edges() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { if (check_node(i)) { return true; } } } return false; } int first() { for(int k = 1; k <= n; ++k) { if (t[k].edge_n == 1) { return k; } } return 1; } int calc_changes(char *c) { int k; int res = 0; char lastc; int prev; for(k = 1; k <= n; ++k) { if (t[k].edge_n == 1) { break; } } lastc = c[k]; prev = k; k = t[k].e[0]; if (c[k] != lastc) { res++; } lastc = c[k]; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; if (c[k] != lastc) { res++; } lastc = c[k]; } return res; } int main() { int nt; scanf("%d\n", &nt); for(int i = 0; i < nt; ++i) { node nd; scanf("%d\n", &n); scanf("%s\n", ca); scanf("%s\n", cb); for(int j = n; j > 0; --j) { ca[j] = ca[j-1]; cb[j] = cb[j-1]; } t.clear(); t.resize(n+1); for(int j = 1; j <= n; ++j) { t[j].nr=j; t[j].edge_n=0; t[j].e.clear(); } for(int j = 0; j < n-1; ++j) { int a,b; scanf("%d %d\n", &a, &b); t[a].edge_n++; t[a].e.push_back(b); t[b].edge_n++; t[b].e.push_back(a); } if (input_equal_output()) { printf("TAK\n"); continue; } if (same_colors_in_input()) { printf("NIE\n"); continue; } if (exists_node_with_morethan2_edges_with_diff_adj_colours()) { printf("TAK\n"); continue; } // nie ma w�z�ow o wi�cej ni� 2 s�siadach // albo // s� w�z�y x, o wi�cej ni� 2 s�siadach, ale wtedy wszyscy s�siedzi maj� ten sam kolor, inny ni� w�ze� x if (exists_node_with_morethan2_edges()) { // funkcja bada w�z�y o wi�cej ni� 2 s�siadach // jesli taki w�ze� i ma s�siada j // oraz w�ze� j ma s�siada k (innego ni� i) w tym samym kolorze co j to true // oraz w�ze� j ma s�siada k (innego ni� i) w innym kolorze ni� j, ale ma kolejnego s�siada w tym samym kolorze co k to true if (check_nodes_with_morethan2_edges()) { printf("TAK\n"); } else { printf("NIE\n"); } continue; } //4) je�li istniej� w�z�y (i) o > 2 s�siadach i wszyscy sasiedzi (j) maj� inny kolor ni� w�ze� to: // 1) je�li istnieje s�siad j kt�ry ma s�siada (k) (innego ni� i) w tym samym kolorze to TAK // 2) je��i istnieje s�siad j kt�rym ma s�siada k, oraz k ma kolejnego s�siada m w tym samym kolorze co k to TAK // 3) w p.p. NIE // 5) pozosta�y ju� tylko przypadki zdegenerowane do prostej int chga = calc_changes(ca); int chgb = calc_changes(cb); if (chga < chgb) { printf("NIE\n"); continue; } if (chga > chgb) { printf("TAK\n"); continue; } if (ca[first()] == cb[first()]) { printf("TAK\n"); } else { printf("NIE\n"); } } 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 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #include <cstdio> #include <cassert> #include <vector> #include <tuple> #include <unordered_set> #include <queue> #include <chrono> using namespace std; const int maxn = 100005; int n; char ca[maxn]; char cb[maxn]; class node { public: int nr; int edge_n; vector<int> e; }; vector<node> t; void print_tree() { printf("\n********************\n"); for(int i = 1; i <= n; ++i) { printf("(%d ca:%c cb:%c)", t[i].nr, ca[i], cb[i]); for(int j = 0; j < t[i].edge_n; ++j) { printf(" -> %d", t[i].e[j]); } printf("\n"); } // if (r[k] == 0) { // printf("NIE\n"); // } else { // printf("TAK\n"); // } } bool same_colors_in_input() { char c = ca[1]; for(int i = 1; i <= n; ++i) { if (ca[i] != c) { return false; } } return true; } bool input_equal_output() { for(int i = 1; i <= n; ++i) { if (ca[i] != cb[i]) { return false; } } return true; } bool adjacent_nodes_have_different_color_than_me_at_finish(int k) { char mycolor = cb[t[k].nr]; for(int i = 0; i < t[k].edge_n; ++i) { if (cb[t[k].e[i]] == mycolor) { return false; } } return true; } bool exists_node_with_morethan2_edges_with_diff_adj_colours() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { if (!adjacent_nodes_have_different_color_than_me_at_finish(i)) { return true; } } } return false; } bool exists_node_with_morethan2_edges() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { return true; } } return false; } int calc_changes(int k, int prev) { int res = 0; char lastc; lastc = cb[k]; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; if (cb[k] != lastc) { res++; } lastc = cb[k]; } return res; } int calc_len(int k, int prev) { int res = 0; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; res++; } return res; } // if adj of k have different colours return true // // exists adj of k (called j) that have another adj (!= k) having same colur as j bool check_node(int k) { char c = cb[t[k].e[0]]; for(int i = 0; i < t[k].edge_n; ++i) { if (cb[t[k].e[i]] != c) { return false; } } // all adj have same colours for(int i = 0; i < t[k].edge_n; ++i) { int j = t[k].e[i]; for(int l = 0; l < t[j].edge_n; ++l) { int m = t[j].e[l]; if (m != k) { if (cb[m] == cb[j]) { return true; } for(int i2 = 0; i2 < t[m].edge_n; ++i2) { int j2 = t[m].e[i2]; if (j2 != m) { if (cb[j2] == cb[m]) { return true; } } } if (calc_changes(m, j) < calc_len(m, j)) { return true; } } } } return false; } bool check_nodes_with_morethan2_edges() { for(int i = 1; i <= n; ++i) { if (t[i].edge_n > 2) { if (check_node(i)) { return true; } } } return false; } int first() { for(int k = 1; k <= n; ++k) { if (t[k].edge_n == 1) { return k; } } return 1; } int calc_changes(char *c) { int k; int res = 0; char lastc; int prev; for(k = 1; k <= n; ++k) { if (t[k].edge_n == 1) { break; } } lastc = c[k]; prev = k; k = t[k].e[0]; if (c[k] != lastc) { res++; } lastc = c[k]; while(1) { int next = t[k].e[0]; if (next == prev) { if (t[k].edge_n == 1) { break; } next = t[k].e[1]; } prev = k; k = next; if (c[k] != lastc) { res++; } lastc = c[k]; } return res; } int main() { int nt; scanf("%d\n", &nt); for(int i = 0; i < nt; ++i) { node nd; scanf("%d\n", &n); scanf("%s\n", ca); scanf("%s\n", cb); for(int j = n; j > 0; --j) { ca[j] = ca[j-1]; cb[j] = cb[j-1]; } t.clear(); t.resize(n+1); for(int j = 1; j <= n; ++j) { t[j].nr=j; t[j].edge_n=0; t[j].e.clear(); } for(int j = 0; j < n-1; ++j) { int a,b; scanf("%d %d\n", &a, &b); t[a].edge_n++; t[a].e.push_back(b); t[b].edge_n++; t[b].e.push_back(a); } if (input_equal_output()) { printf("TAK\n"); continue; } if (same_colors_in_input()) { printf("NIE\n"); continue; } if (exists_node_with_morethan2_edges_with_diff_adj_colours()) { printf("TAK\n"); continue; } // nie ma w�z�ow o wi�cej ni� 2 s�siadach // albo // s� w�z�y x, o wi�cej ni� 2 s�siadach, ale wtedy wszyscy s�siedzi maj� ten sam kolor, inny ni� w�ze� x if (exists_node_with_morethan2_edges()) { // funkcja bada w�z�y o wi�cej ni� 2 s�siadach // jesli taki w�ze� i ma s�siada j // oraz w�ze� j ma s�siada k (innego ni� i) w tym samym kolorze co j to true // oraz w�ze� j ma s�siada k (innego ni� i) w innym kolorze ni� j, ale ma kolejnego s�siada w tym samym kolorze co k to true if (check_nodes_with_morethan2_edges()) { printf("TAK\n"); } else { printf("NIE\n"); } continue; } //4) je�li istniej� w�z�y (i) o > 2 s�siadach i wszyscy sasiedzi (j) maj� inny kolor ni� w�ze� to: // 1) je�li istnieje s�siad j kt�ry ma s�siada (k) (innego ni� i) w tym samym kolorze to TAK // 2) je��i istnieje s�siad j kt�rym ma s�siada k, oraz k ma kolejnego s�siada m w tym samym kolorze co k to TAK // 3) w p.p. NIE // 5) pozosta�y ju� tylko przypadki zdegenerowane do prostej int chga = calc_changes(ca); int chgb = calc_changes(cb); if (chga < chgb) { printf("NIE\n"); continue; } if (chga > chgb) { printf("TAK\n"); continue; } if (ca[first()] == cb[first()]) { printf("TAK\n"); } else { printf("NIE\n"); } } return 0; } |