#include <bits/stdc++.h> using namespace std; bool comparePairs(pair<int, int> &p1, pair<int, int> &p2){ return p1.first == p2.first ? p1.second < p2.second : p1.first < p2.first; } int zones(vector<vector<int>> &edges, const string& colors, int begin=0, int root=-1){ queue <int> toVisit; bool *visited = new bool[edges.size()]; for(int i = 0; i < edges.size(); i++) visited[i] = false; int result = 1; toVisit.push(begin); visited[begin] = true; while (!toVisit.empty()) { int nextV = toVisit.front(); toVisit.pop(); for(auto neighbour: edges[nextV]){ if(neighbour == root) continue; if(visited[neighbour]) continue; toVisit.push(neighbour); visited[neighbour] = true; if(colors[neighbour] != colors[nextV]) { result++; } } } delete [] visited; return result; } bool hasSameColorKid(vector<vector<int>> &edges, const string& endColors, int root, int kid, int depth=1){ for(auto neighbour: edges[kid]){ if(neighbour == root) continue; if(endColors[neighbour] == endColors[kid]) return true; } // if(depth >= 1) { for(auto neighbour: edges[kid]){ if(neighbour == root) continue; if(hasSameColorKid(edges, endColors, kid, neighbour, 0)) return true; } // } return false; } bool hasUniversalVertex(vector<vector<int>> &edges, const string& endColors, const string& baseColors) { bool twoSameColorNeighbours = hasSameColorKid(edges, endColors, -1, 0); for(int i = 0; i < edges.size(); i++) { int end0 = 0; int end1 = 0; if(edges[i].size() > 2) { for(auto neighbour: edges[i]) { if(endColors[neighbour] == '1') end1++; else end0++; } if(end1 > 0 and end0 > 0) return true; if(end1 > 0 and endColors[i] == '1') return true; if(end0 > 0 and endColors[i] == '0') return true; if(twoSameColorNeighbours) return true; // for(auto neighbour: edges[i]) { // if(hasSameColorKid(edges, endColors, i, neighbour)) // return true; // } } } return false; } //int needAnything(vector<vector<int>> &edges, const string& baseColors, const string& endColors, int kid, int root){ // vector<int> kidsNeeds; // // if(edges[kid].size() == 1){ // if(endColors[kid] == baseColors[kid]) // return -1; // return endColors[kid]; // } // vector<int> kidsWith0; // vector<int> kidsWith1; // vector<int> kidsThatDontNeed; // // for(int i = 0; i < edges[kid].size(); i++) { // if(edges[kid][i] != root) { // int kidNeed = needAnything(edges, baseColors, endColors, edges[kid][i], kid); // kidsNeeds.push_back(kidNeed); // if(kidNeed == -1){ // if(endColors[i] == '1') // kidsWith1.push_back(i); // else // kidsWith0.push_back(i); // } // } // } // // // // // return 0; //} void solve() { int n; cin >> n; string baseColors, endColors; cin >> baseColors; cin >> endColors; vector<vector<int>> edges; for(int i = 0; i < n; i++){ vector<int> tmp; edges.push_back(tmp); } for(int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; edges[a-1].push_back(b-1); edges[b-1].push_back(a-1); } if(baseColors == endColors){ cout << "TAK" << endl; return; } int redCounter = 0; int blackCounter = 0; for(char baseColor : baseColors){ if(baseColor == '0') redCounter++; else blackCounter++; } if(redCounter == 0 or blackCounter == 0){ cout << "NIE" << endl; return; } if(hasUniversalVertex(edges, endColors, baseColors)) { cout << "TAK" << endl; return; } int baseZones = zones(edges, baseColors); int endZones = zones(edges, endColors); // cout<< baseZones << " " << endZones << endl; if(endZones > baseZones) { cout << "NIE" << endl; return; } vector<int> ends; for(int i = 0; i < edges.size(); i++){ if(edges[i].size() == 1) ends.push_back(i); } if(endZones == baseZones and baseColors[ends.front()] != endColors[ends.front()]) { cout << "NIE" << endl; return; } cout << "TAK"<<endl; } int main() { std::ios_base::sync_with_stdio(false); int t; cin >> t; for(int i = 0; i < t; i++) solve(); 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 | #include <bits/stdc++.h> using namespace std; bool comparePairs(pair<int, int> &p1, pair<int, int> &p2){ return p1.first == p2.first ? p1.second < p2.second : p1.first < p2.first; } int zones(vector<vector<int>> &edges, const string& colors, int begin=0, int root=-1){ queue <int> toVisit; bool *visited = new bool[edges.size()]; for(int i = 0; i < edges.size(); i++) visited[i] = false; int result = 1; toVisit.push(begin); visited[begin] = true; while (!toVisit.empty()) { int nextV = toVisit.front(); toVisit.pop(); for(auto neighbour: edges[nextV]){ if(neighbour == root) continue; if(visited[neighbour]) continue; toVisit.push(neighbour); visited[neighbour] = true; if(colors[neighbour] != colors[nextV]) { result++; } } } delete [] visited; return result; } bool hasSameColorKid(vector<vector<int>> &edges, const string& endColors, int root, int kid, int depth=1){ for(auto neighbour: edges[kid]){ if(neighbour == root) continue; if(endColors[neighbour] == endColors[kid]) return true; } // if(depth >= 1) { for(auto neighbour: edges[kid]){ if(neighbour == root) continue; if(hasSameColorKid(edges, endColors, kid, neighbour, 0)) return true; } // } return false; } bool hasUniversalVertex(vector<vector<int>> &edges, const string& endColors, const string& baseColors) { bool twoSameColorNeighbours = hasSameColorKid(edges, endColors, -1, 0); for(int i = 0; i < edges.size(); i++) { int end0 = 0; int end1 = 0; if(edges[i].size() > 2) { for(auto neighbour: edges[i]) { if(endColors[neighbour] == '1') end1++; else end0++; } if(end1 > 0 and end0 > 0) return true; if(end1 > 0 and endColors[i] == '1') return true; if(end0 > 0 and endColors[i] == '0') return true; if(twoSameColorNeighbours) return true; // for(auto neighbour: edges[i]) { // if(hasSameColorKid(edges, endColors, i, neighbour)) // return true; // } } } return false; } //int needAnything(vector<vector<int>> &edges, const string& baseColors, const string& endColors, int kid, int root){ // vector<int> kidsNeeds; // // if(edges[kid].size() == 1){ // if(endColors[kid] == baseColors[kid]) // return -1; // return endColors[kid]; // } // vector<int> kidsWith0; // vector<int> kidsWith1; // vector<int> kidsThatDontNeed; // // for(int i = 0; i < edges[kid].size(); i++) { // if(edges[kid][i] != root) { // int kidNeed = needAnything(edges, baseColors, endColors, edges[kid][i], kid); // kidsNeeds.push_back(kidNeed); // if(kidNeed == -1){ // if(endColors[i] == '1') // kidsWith1.push_back(i); // else // kidsWith0.push_back(i); // } // } // } // // // // // return 0; //} void solve() { int n; cin >> n; string baseColors, endColors; cin >> baseColors; cin >> endColors; vector<vector<int>> edges; for(int i = 0; i < n; i++){ vector<int> tmp; edges.push_back(tmp); } for(int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; edges[a-1].push_back(b-1); edges[b-1].push_back(a-1); } if(baseColors == endColors){ cout << "TAK" << endl; return; } int redCounter = 0; int blackCounter = 0; for(char baseColor : baseColors){ if(baseColor == '0') redCounter++; else blackCounter++; } if(redCounter == 0 or blackCounter == 0){ cout << "NIE" << endl; return; } if(hasUniversalVertex(edges, endColors, baseColors)) { cout << "TAK" << endl; return; } int baseZones = zones(edges, baseColors); int endZones = zones(edges, endColors); // cout<< baseZones << " " << endZones << endl; if(endZones > baseZones) { cout << "NIE" << endl; return; } vector<int> ends; for(int i = 0; i < edges.size(); i++){ if(edges[i].size() == 1) ends.push_back(i); } if(endZones == baseZones and baseColors[ends.front()] != endColors[ends.front()]) { cout << "NIE" << endl; return; } cout << "TAK"<<endl; } int main() { std::ios_base::sync_with_stdio(false); int t; cin >> t; for(int i = 0; i < t; i++) solve(); return 0; } |