#include <unistd.h> #include <string> #include <vector> #include <map> #include <deque> #include <iostream> #include <algorithm> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = vector<int>; using VLL = vector<LL>; using VULL = vector<ULL>; // parser by tczajka class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } template<> ULL Input::Get<ULL>() { SkipWS(); ULL x = 0; while(isdigit(UPeek())) { x = 10ULL * x + (Get<char>()-'0'); } return x; } template<> LL Input::Get<LL>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } ULL x = Get<ULL>(); if (neg) x = -x; return static_cast<LL>(x); } template<> string Input::Get<string>() { SkipWS(); string s; while(!Eof() && !isspace(UPeek())) s += Get<char>(); return s; } struct Node { vector<int> next_nodes; }; Input IN; int main() { auto n = IN.Get<int>(); REP(i, n) { auto ds = IN.Get<int>(); vector<int> v1, v2; v1.reserve(ds); v2.reserve(ds); bool has_red1 = false; bool has_red2 = false; bool has_black1 = false; bool has_black2 = false; IN.SkipWS(); REP(j, ds) { bool is_black = IN.Get<char>() == '1'; v1.push_back(is_black); has_black1 |= is_black; has_red1 |= (!is_black); } IN.SkipWS(); REP(j, ds) { bool is_black = IN.Get<char>() == '1'; v2.push_back(is_black); has_black2 |= is_black; has_red2 |= (!is_black); } vector<Node> nodes; nodes.resize(ds); REP(j, ds-1) { auto b = IN.Get<int>(); auto e = IN.Get<int>(); nodes[b-1].next_nodes.push_back(e-1); nodes[e-1].next_nodes.push_back(b-1); } if(ds==1) { if (v1[0] == v2[0]) cout << "TAK" << endl; else cout << "NIE" << endl; continue; } if (v1 == v2) { cout << "TAK" << endl; continue; } // nie ma czarnego/czerwonego a bedzie if ((!has_black1 && has_black2) || (!has_red1 && has_red2)) { cout << "NIE" << endl; continue; } // same czerwone albo sam czarne na koncu if ((has_black1 && has_red1) && (!has_black2 || !has_red2)) { cout << "TAK" << endl; continue; } // same czerwone albo sam czarne if ((!has_black1 && !has_black2) || (!has_red1 && !has_red2)) { cout << "TAK" << endl; continue; } std::vector<int> leafs; REP(j, nodes.size()) { if (nodes[j].next_nodes.size() == 1) leafs.push_back(j); } // nie linia if (leafs.size() > 2) { bool found = false; REP(j, nodes.size()) { for (auto & nn : nodes[j].next_nodes) { if (v2[j] == v2[nn]) { found = true; break; } } if(found) break; } if (found) cout << "TAK" << endl; else cout << "NIE" << endl; continue; } // sprawdzamy linie, liczymy ilosc ciaglych przestrzeni int continues_colors1 = 1; int continues_colors2 = 1; int b_node = leafs[0]; int e_node = leafs[1]; int current_node = b_node; int prev_node = -1; int last_color1 = v1[current_node]; int last_color2 = v2[current_node]; while (current_node != e_node) { // find next node for (auto nn : nodes[current_node].next_nodes){ if (nn == prev_node) continue; // next one prev_node = current_node; current_node = nn; break; } if (v1[current_node] != last_color1) { continues_colors1++; last_color1 = v1[current_node]; } if (v2[current_node] != last_color2) { continues_colors2++; last_color2 = v2[current_node]; } } if (continues_colors2 > continues_colors1) { cout << "NIE" << endl; continue; } if (continues_colors2 < continues_colors1) { cout << "TAK" << endl; continue; } // tyle samo ciaglych kolorow, musza sie zgadzac pierwszy if (v1[b_node] == v2[b_node]) cout << "TAK" << endl; else cout << "NIE" << endl; } 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 | #include <unistd.h> #include <string> #include <vector> #include <map> #include <deque> #include <iostream> #include <algorithm> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = vector<int>; using VLL = vector<LL>; using VULL = vector<ULL>; // parser by tczajka class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } template<> ULL Input::Get<ULL>() { SkipWS(); ULL x = 0; while(isdigit(UPeek())) { x = 10ULL * x + (Get<char>()-'0'); } return x; } template<> LL Input::Get<LL>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } ULL x = Get<ULL>(); if (neg) x = -x; return static_cast<LL>(x); } template<> string Input::Get<string>() { SkipWS(); string s; while(!Eof() && !isspace(UPeek())) s += Get<char>(); return s; } struct Node { vector<int> next_nodes; }; Input IN; int main() { auto n = IN.Get<int>(); REP(i, n) { auto ds = IN.Get<int>(); vector<int> v1, v2; v1.reserve(ds); v2.reserve(ds); bool has_red1 = false; bool has_red2 = false; bool has_black1 = false; bool has_black2 = false; IN.SkipWS(); REP(j, ds) { bool is_black = IN.Get<char>() == '1'; v1.push_back(is_black); has_black1 |= is_black; has_red1 |= (!is_black); } IN.SkipWS(); REP(j, ds) { bool is_black = IN.Get<char>() == '1'; v2.push_back(is_black); has_black2 |= is_black; has_red2 |= (!is_black); } vector<Node> nodes; nodes.resize(ds); REP(j, ds-1) { auto b = IN.Get<int>(); auto e = IN.Get<int>(); nodes[b-1].next_nodes.push_back(e-1); nodes[e-1].next_nodes.push_back(b-1); } if(ds==1) { if (v1[0] == v2[0]) cout << "TAK" << endl; else cout << "NIE" << endl; continue; } if (v1 == v2) { cout << "TAK" << endl; continue; } // nie ma czarnego/czerwonego a bedzie if ((!has_black1 && has_black2) || (!has_red1 && has_red2)) { cout << "NIE" << endl; continue; } // same czerwone albo sam czarne na koncu if ((has_black1 && has_red1) && (!has_black2 || !has_red2)) { cout << "TAK" << endl; continue; } // same czerwone albo sam czarne if ((!has_black1 && !has_black2) || (!has_red1 && !has_red2)) { cout << "TAK" << endl; continue; } std::vector<int> leafs; REP(j, nodes.size()) { if (nodes[j].next_nodes.size() == 1) leafs.push_back(j); } // nie linia if (leafs.size() > 2) { bool found = false; REP(j, nodes.size()) { for (auto & nn : nodes[j].next_nodes) { if (v2[j] == v2[nn]) { found = true; break; } } if(found) break; } if (found) cout << "TAK" << endl; else cout << "NIE" << endl; continue; } // sprawdzamy linie, liczymy ilosc ciaglych przestrzeni int continues_colors1 = 1; int continues_colors2 = 1; int b_node = leafs[0]; int e_node = leafs[1]; int current_node = b_node; int prev_node = -1; int last_color1 = v1[current_node]; int last_color2 = v2[current_node]; while (current_node != e_node) { // find next node for (auto nn : nodes[current_node].next_nodes){ if (nn == prev_node) continue; // next one prev_node = current_node; current_node = nn; break; } if (v1[current_node] != last_color1) { continues_colors1++; last_color1 = v1[current_node]; } if (v2[current_node] != last_color2) { continues_colors2++; last_color2 = v2[current_node]; } } if (continues_colors2 > continues_colors1) { cout << "NIE" << endl; continue; } if (continues_colors2 < continues_colors1) { cout << "TAK" << endl; continue; } // tyle samo ciaglych kolorow, musza sie zgadzac pierwszy if (v1[b_node] == v2[b_node]) cout << "TAK" << endl; else cout << "NIE" << endl; } return 0; } |