#define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <iostream> #include <string> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) { int n; scanf("%d", &n); string kolorA; string kolorB; getline(std::cin, kolorA); getline(std::cin, kolorA); getline(std::cin, kolorB); // łi nid to do graf ----- siiiiiii vector<vector<int>> edges(n); for (int j = 0; j < n - 1; ++j) { int a, b; scanf("%d %d", &a, &b); --a; --b; edges[a].push_back(b); edges[b].push_back(a); } // czek kolors - simple hełreza na beginin int akc[2] = {0,0}; int bkc[2] = { 0,0 }; for (int j = 0; j < n; ++j) { akc[kolorA[j] - '0']= 1; bkc[kolorB[j] - '0']= 1; } int akcs = akc[0] + akc[1]; int bkcs = bkc[0] + bkc[1]; if (kolorA == kolorB) { printf("TAK\n"); continue; } if (akcs < bkcs) { printf("NIE\n"); continue; } if (akcs > bkcs) { printf("TAK\n"); continue; } if (akcs == 1) { if (akc[0] != bkc[0]) { printf("NIE\n"); continue; } else { printf("TAK\n"); continue; } } /// stejdz tfu bool drzewko = false; int listek = -1; for (int j = 0; j < n; ++j) { if (edges[j].size() > 2) { drzewko = true; } if (edges[j].size() == 1) { listek = j; } } if (drzewko) { bool wynik = false; for (int j = 0; j < n; ++j) { char mycolor = kolorB[j]; for (int k = 0; k < edges[j].size(); ++k) { char odderkolor = kolorB[edges[j][k]]; if (mycolor == odderkolor) { wynik = true; goto jessssss; } } } jessssss: if (wynik==false) { printf("NIE\n"); continue; } else { printf("TAK\n"); continue; } } else { int head = listek; int prevhead=listek; int nn = n; int ac = 0; int bc = 0; char al = 'x'; char bl = 'x'; while (nn-- > 0) { int next = -1; for (int j = 0; j < edges[head].size(); ++j) { if (edges[head][j] != prevhead) { next = edges[head][j]; } } if (al != kolorA[head]) { ac++; al = kolorA[head]; } if (bl != kolorB[head]) { bc++; bl = kolorB[head]; } prevhead = head; head = next; } if (ac > bc) { printf("TAK\n"); continue; } if (ac == bc && kolorA[listek] == kolorB[listek]) { printf("TAK\n"); continue; } printf("NIE\n"); continue; } printf("TAK\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 | #define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <iostream> #include <string> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> using namespace std; int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) { int n; scanf("%d", &n); string kolorA; string kolorB; getline(std::cin, kolorA); getline(std::cin, kolorA); getline(std::cin, kolorB); // łi nid to do graf ----- siiiiiii vector<vector<int>> edges(n); for (int j = 0; j < n - 1; ++j) { int a, b; scanf("%d %d", &a, &b); --a; --b; edges[a].push_back(b); edges[b].push_back(a); } // czek kolors - simple hełreza na beginin int akc[2] = {0,0}; int bkc[2] = { 0,0 }; for (int j = 0; j < n; ++j) { akc[kolorA[j] - '0']= 1; bkc[kolorB[j] - '0']= 1; } int akcs = akc[0] + akc[1]; int bkcs = bkc[0] + bkc[1]; if (kolorA == kolorB) { printf("TAK\n"); continue; } if (akcs < bkcs) { printf("NIE\n"); continue; } if (akcs > bkcs) { printf("TAK\n"); continue; } if (akcs == 1) { if (akc[0] != bkc[0]) { printf("NIE\n"); continue; } else { printf("TAK\n"); continue; } } /// stejdz tfu bool drzewko = false; int listek = -1; for (int j = 0; j < n; ++j) { if (edges[j].size() > 2) { drzewko = true; } if (edges[j].size() == 1) { listek = j; } } if (drzewko) { bool wynik = false; for (int j = 0; j < n; ++j) { char mycolor = kolorB[j]; for (int k = 0; k < edges[j].size(); ++k) { char odderkolor = kolorB[edges[j][k]]; if (mycolor == odderkolor) { wynik = true; goto jessssss; } } } jessssss: if (wynik==false) { printf("NIE\n"); continue; } else { printf("TAK\n"); continue; } } else { int head = listek; int prevhead=listek; int nn = n; int ac = 0; int bc = 0; char al = 'x'; char bl = 'x'; while (nn-- > 0) { int next = -1; for (int j = 0; j < edges[head].size(); ++j) { if (edges[head][j] != prevhead) { next = edges[head][j]; } } if (al != kolorA[head]) { ac++; al = kolorA[head]; } if (bl != kolorB[head]) { bc++; bl = kolorB[head]; } prevhead = head; head = next; } if (ac > bc) { printf("TAK\n"); continue; } if (ac == bc && kolorA[listek] == kolorB[listek]) { printf("TAK\n"); continue; } printf("NIE\n"); continue; } printf("TAK\n"); } return 0; } |