#include <iostream> #include <vector> #include <string> using namespace std; void readEmpty(int n){ for (int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; } } int main(){ int t; cin >> t; while (t--){ int n; cin >> n; string s, t; cin >> s; cin >> t; bool allBlack1 = true, allRed1 = true, allBlack2 = true, allRed2 = true; // cout << "les " << n << "\n"; for (int i = 0; i < n; i++){ if (s[i] == '0'){ allRed1 = false; } if (s[i] == '1'){ allBlack1 = false; } if (t[i] == '0'){ allRed2 = false; } if (t[i] == '1'){ allBlack2 = false; } } if ((allRed1) && (!allRed2)){ cout << "NIE\n"; readEmpty(n); continue; } if ((allRed1) && (allRed2)){ cout << "TAK\n"; readEmpty(n); continue; } if ((allBlack1) && (!allBlack2)){ cout << "NIE\n"; readEmpty(n); continue; } if ((allBlack1) && (allBlack2)){ cout << "TAK\n"; readEmpty(n); continue; } vector<int>* v = new vector<int> [n + 5]; for (int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } bool ok = false; int p, q = -1; //cout << "bum " << n << "\n"; for (int i = 1; i <= n; i++){ if (v[i].size() > 2){ ok = true; // cout << "e " << v[i].size() << " " << n << "\n"; break; } if (v[i].size() == 1){ p = i; } } if (ok){ bool non_di_color = false; for (int i = 1; i <= n; i++){ for (int j = 0; j < v[i].size(); j++){ if (t[i - 1] == t[v[i][j] - 1]){ non_di_color = true; break; } } } if (non_di_color){ cout << "TAK\n"; } else { bool lastChance = true; for (int i = 1; i <= n; i++){ if (s[i - 1] != t[i - 1]){ lastChance = false; break; } } if (lastChance){ cout << "TAK\n"; } else { cout << "NIE\n"; } } delete [] v; continue; } int noChanges1 = 0, noChanges2 = 0; int singles = 0; while (true){ // cout << p << " "; if (v[p].size() == 1){ singles++; } if (singles == 2){ break; } int r; for (int i = 0; i < v[p].size(); i++){ if (v[p][i] != q){ r = v[p][i]; } } if (s[p - 1] != s[r - 1]){ noChanges1++; } if (t[p - 1] != t[r - 1]){ noChanges2++; } q = p; p = r; } //cout << "\na " << noChanges1 << " " << noChanges2 << "\n"; if (noChanges1 > noChanges2){ cout << "TAK\n"; } else if ((noChanges1 == noChanges2) && (s[p - 1] == t[p - 1])){ cout << "TAK\n"; } else { cout << "NIE\n"; } delete [] v; } }
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 | #include <iostream> #include <vector> #include <string> using namespace std; void readEmpty(int n){ for (int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; } } int main(){ int t; cin >> t; while (t--){ int n; cin >> n; string s, t; cin >> s; cin >> t; bool allBlack1 = true, allRed1 = true, allBlack2 = true, allRed2 = true; // cout << "les " << n << "\n"; for (int i = 0; i < n; i++){ if (s[i] == '0'){ allRed1 = false; } if (s[i] == '1'){ allBlack1 = false; } if (t[i] == '0'){ allRed2 = false; } if (t[i] == '1'){ allBlack2 = false; } } if ((allRed1) && (!allRed2)){ cout << "NIE\n"; readEmpty(n); continue; } if ((allRed1) && (allRed2)){ cout << "TAK\n"; readEmpty(n); continue; } if ((allBlack1) && (!allBlack2)){ cout << "NIE\n"; readEmpty(n); continue; } if ((allBlack1) && (allBlack2)){ cout << "TAK\n"; readEmpty(n); continue; } vector<int>* v = new vector<int> [n + 5]; for (int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } bool ok = false; int p, q = -1; //cout << "bum " << n << "\n"; for (int i = 1; i <= n; i++){ if (v[i].size() > 2){ ok = true; // cout << "e " << v[i].size() << " " << n << "\n"; break; } if (v[i].size() == 1){ p = i; } } if (ok){ bool non_di_color = false; for (int i = 1; i <= n; i++){ for (int j = 0; j < v[i].size(); j++){ if (t[i - 1] == t[v[i][j] - 1]){ non_di_color = true; break; } } } if (non_di_color){ cout << "TAK\n"; } else { bool lastChance = true; for (int i = 1; i <= n; i++){ if (s[i - 1] != t[i - 1]){ lastChance = false; break; } } if (lastChance){ cout << "TAK\n"; } else { cout << "NIE\n"; } } delete [] v; continue; } int noChanges1 = 0, noChanges2 = 0; int singles = 0; while (true){ // cout << p << " "; if (v[p].size() == 1){ singles++; } if (singles == 2){ break; } int r; for (int i = 0; i < v[p].size(); i++){ if (v[p][i] != q){ r = v[p][i]; } } if (s[p - 1] != s[r - 1]){ noChanges1++; } if (t[p - 1] != t[r - 1]){ noChanges2++; } q = p; p = r; } //cout << "\na " << noChanges1 << " " << noChanges2 << "\n"; if (noChanges1 > noChanges2){ cout << "TAK\n"; } else if ((noChanges1 == noChanges2) && (s[p - 1] == t[p - 1])){ cout << "TAK\n"; } else { cout << "NIE\n"; } delete [] v; } } |