#include <iostream> #include <vector> char word1[100'005]; char word2[100'005]; struct pkt { bool v; bool c; bool d; std::vector <int> e; }; void tc(int t) { int n; std::cin >> n; std::vector <pkt> w; w.resize(n); bool jest_zero = false; bool jest_jeden = false; bool chce_zero = false; bool chce_jeden = false; std::cin >> word1 >> word2; for (int i = 0; i < n; ++i) { bool jest = word1[i] == '1'; w[i].c = jest; jest_zero |= !jest; jest_jeden |= jest; bool chce = word2[i] == '1'; w[i].d = chce; chce_zero |= !chce; chce_jeden |= chce; w[i].v = false; } bool kmmtss = false; // ktos ma miec takiego samego somsiada xd bool kmjtss = false; // -||- juz -||- for (int i = 0; i < (n - 1); ++i) { int a, b; std::cin >> a >> b; --a; --b; kmmtss |= (w[a].d == w[b].d); kmjtss |= (w[a].c == w[b].c); w[a].e.push_back(b); w[b].e.push_back(a); } if (!kmmtss) { std::cout << ((kmjtss || (w[0].c != w[0].d)) ? "NIE" : "TAK") << std::endl; return; } if (!chce_zero) { std::cout << (jest_jeden ? "TAK" : "NIE") << std::endl; return; } if (!chce_jeden) { std::cout << (jest_zero ? "TAK" : "NIE") << std::endl; return; } // chce oba, ktoregos nie ma if (!jest_zero || !jest_jeden) { std::cout << "NIE" << std::endl; return; } bool trzy = false; int jedynka = 0; for (int i = 0; i < n; ++i) { int s = w[i].e.size(); if (s > 2) { std::cout << "TAK" << std::endl; return; } if (s == 1) jedynka = i; } bool last_c = w[jedynka].c; bool last_d = w[jedynka].d; int cj = last_c ? 1 : 0; int cz = 1 - cj; int dj = last_d ? 1 : 0; int dz = 1 - dj; int popr = jedynka; int curr = w[jedynka].e[0]; while (w[curr].e.size() == 2) { if (w[curr].c != last_c) { ++(last_c ? cz : cj); last_c = w[curr].c; } if (w[curr].d != last_d) { ++(last_d ? dz : dj); last_d = w[curr].d; } if (w[curr].e[0] == popr) { popr = curr; curr = w[curr].e[1]; } else { popr = curr; curr = w[curr].e[0]; } } if (w[curr].c != last_c) { ++(last_c ? cz : cj); last_c = w[curr].c; } if (w[curr].d != last_d) { ++(last_d ? dz : dj); last_d = w[curr].d; } if (cz < dz || cj < dj) std::cout << "NIE" << std::endl; else if (cz > dz || cj > dj) std::cout << "TAK" << std::endl; else std::cout << (last_c == last_d ? "TAK" : "NIE") << std::endl; } int main() { int t; std::cin >> t; for (int i = 0; i < t; ++i) tc(i); 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 | #include <iostream> #include <vector> char word1[100'005]; char word2[100'005]; struct pkt { bool v; bool c; bool d; std::vector <int> e; }; void tc(int t) { int n; std::cin >> n; std::vector <pkt> w; w.resize(n); bool jest_zero = false; bool jest_jeden = false; bool chce_zero = false; bool chce_jeden = false; std::cin >> word1 >> word2; for (int i = 0; i < n; ++i) { bool jest = word1[i] == '1'; w[i].c = jest; jest_zero |= !jest; jest_jeden |= jest; bool chce = word2[i] == '1'; w[i].d = chce; chce_zero |= !chce; chce_jeden |= chce; w[i].v = false; } bool kmmtss = false; // ktos ma miec takiego samego somsiada xd bool kmjtss = false; // -||- juz -||- for (int i = 0; i < (n - 1); ++i) { int a, b; std::cin >> a >> b; --a; --b; kmmtss |= (w[a].d == w[b].d); kmjtss |= (w[a].c == w[b].c); w[a].e.push_back(b); w[b].e.push_back(a); } if (!kmmtss) { std::cout << ((kmjtss || (w[0].c != w[0].d)) ? "NIE" : "TAK") << std::endl; return; } if (!chce_zero) { std::cout << (jest_jeden ? "TAK" : "NIE") << std::endl; return; } if (!chce_jeden) { std::cout << (jest_zero ? "TAK" : "NIE") << std::endl; return; } // chce oba, ktoregos nie ma if (!jest_zero || !jest_jeden) { std::cout << "NIE" << std::endl; return; } bool trzy = false; int jedynka = 0; for (int i = 0; i < n; ++i) { int s = w[i].e.size(); if (s > 2) { std::cout << "TAK" << std::endl; return; } if (s == 1) jedynka = i; } bool last_c = w[jedynka].c; bool last_d = w[jedynka].d; int cj = last_c ? 1 : 0; int cz = 1 - cj; int dj = last_d ? 1 : 0; int dz = 1 - dj; int popr = jedynka; int curr = w[jedynka].e[0]; while (w[curr].e.size() == 2) { if (w[curr].c != last_c) { ++(last_c ? cz : cj); last_c = w[curr].c; } if (w[curr].d != last_d) { ++(last_d ? dz : dj); last_d = w[curr].d; } if (w[curr].e[0] == popr) { popr = curr; curr = w[curr].e[1]; } else { popr = curr; curr = w[curr].e[0]; } } if (w[curr].c != last_c) { ++(last_c ? cz : cj); last_c = w[curr].c; } if (w[curr].d != last_d) { ++(last_d ? dz : dj); last_d = w[curr].d; } if (cz < dz || cj < dj) std::cout << "NIE" << std::endl; else if (cz > dz || cj > dj) std::cout << "TAK" << std::endl; else std::cout << (last_c == last_d ? "TAK" : "NIE") << std::endl; } int main() { int t; std::cin >> t; for (int i = 0; i < t; ++i) tc(i); return 0; } |