#include <iostream> #include <vector> using namespace std; void readCols(vector<bool>& res, int n) { for(int i = 1; i <= n; ++i) { char c; cin >> c; if(c == '1') { res[i] = true; } } } pair<int, pair<int, int> > findV(const vector<vector<int> >& G, const vector<bool>& cols, int n) { pair<int, pair<int, int> > res(-1, {-1, -1}); for(int i = 1; i <= n; ++i) { if(G[i].size() >= 3) { for(int j = 0; j < G[i].size(); ++j) { if(cols[G[i][j]] != cols[i]) { if(res.second.first == -1) { res.first = i, res.second.first = G[i][j]; } } else { if(res.second.second == -1) { res.second.second = G[i][j]; } } if(res.second.first != -1 && res.second.second != -1) { return res; } } } } return res; } void propagate(const vector<vector<int> >& G, vector<bool> & cols, int v, int prev, int type = 0) { for(auto u : G[v]) { if(u != prev) { if(type == 0) { cols[u] = !cols[v]; } else { type = 0; cols[u] = cols[v]; } propagate(G, cols, u, v); } } } void recolor(const vector<vector<int> >& G, vector<bool>& cols, pair<int, pair<int, int>> center) { bool firstOne = true; int b = center.first, a = center.second.first, c = center.second.second, d; for(auto u : G[b]) { cols[u] = !cols[b]; if(firstOne && u != a && u != c) { propagate(G, cols, u, b, 1); d = u; firstOne = false; } else { propagate(G, cols, u, b); } } if(G[d].size() == 1) { cols[d] = !cols[d]; } } int countCols(const vector<bool>& cols, int n) { int res = 0; for(int i = 1; i <= n; ++i) { res += cols[i]; } return res; } void solve() { int n; cin >> n; vector<bool> col1(n + 1), col2(n + 1); vector<vector<int> > G(n + 1); readCols(col1, n); readCols(col2, n); int diff1 = 0, diff2 = 0; for(int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); if(col1[a] != col1[b]) ++diff1; if(col2[a] != col2[b]) ++diff2; } if(col1 == col2) { cout << "TAK\n"; return; } if(diff1 == n - 1 && diff2 < n - 1) { cout << "TAK\n"; return; } if(diff2 == n - 1) { cout << "NIE\n"; return; } pair<int, pair<int, int> > center = findV(G, col1, n); if(center.first != -1) { //cout << center.first << " " << center.second.first << " " << center.second.second << "\n"; recolor(G, col1, center); diff1 = n - 2; /*for(int i = 1; i <= n; ++i) { cout << col1[i]; } cout << "\n";*/ } int sum1 = countCols(col1, n), sum2 = countCols(col2, n); if(col1 == col2) { cout << "TAK\n"; return; } if(diff1 < diff2) { cout << "NIE\n"; } else { if(diff1 > diff2) { cout << "TAK\n"; } else { cout << (diff1 <= 1 || sum1 == sum2 ? "NIE" : "TAK") << "\n"; } } } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while(t--) { solve(); } }
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> using namespace std; void readCols(vector<bool>& res, int n) { for(int i = 1; i <= n; ++i) { char c; cin >> c; if(c == '1') { res[i] = true; } } } pair<int, pair<int, int> > findV(const vector<vector<int> >& G, const vector<bool>& cols, int n) { pair<int, pair<int, int> > res(-1, {-1, -1}); for(int i = 1; i <= n; ++i) { if(G[i].size() >= 3) { for(int j = 0; j < G[i].size(); ++j) { if(cols[G[i][j]] != cols[i]) { if(res.second.first == -1) { res.first = i, res.second.first = G[i][j]; } } else { if(res.second.second == -1) { res.second.second = G[i][j]; } } if(res.second.first != -1 && res.second.second != -1) { return res; } } } } return res; } void propagate(const vector<vector<int> >& G, vector<bool> & cols, int v, int prev, int type = 0) { for(auto u : G[v]) { if(u != prev) { if(type == 0) { cols[u] = !cols[v]; } else { type = 0; cols[u] = cols[v]; } propagate(G, cols, u, v); } } } void recolor(const vector<vector<int> >& G, vector<bool>& cols, pair<int, pair<int, int>> center) { bool firstOne = true; int b = center.first, a = center.second.first, c = center.second.second, d; for(auto u : G[b]) { cols[u] = !cols[b]; if(firstOne && u != a && u != c) { propagate(G, cols, u, b, 1); d = u; firstOne = false; } else { propagate(G, cols, u, b); } } if(G[d].size() == 1) { cols[d] = !cols[d]; } } int countCols(const vector<bool>& cols, int n) { int res = 0; for(int i = 1; i <= n; ++i) { res += cols[i]; } return res; } void solve() { int n; cin >> n; vector<bool> col1(n + 1), col2(n + 1); vector<vector<int> > G(n + 1); readCols(col1, n); readCols(col2, n); int diff1 = 0, diff2 = 0; for(int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); if(col1[a] != col1[b]) ++diff1; if(col2[a] != col2[b]) ++diff2; } if(col1 == col2) { cout << "TAK\n"; return; } if(diff1 == n - 1 && diff2 < n - 1) { cout << "TAK\n"; return; } if(diff2 == n - 1) { cout << "NIE\n"; return; } pair<int, pair<int, int> > center = findV(G, col1, n); if(center.first != -1) { //cout << center.first << " " << center.second.first << " " << center.second.second << "\n"; recolor(G, col1, center); diff1 = n - 2; /*for(int i = 1; i <= n; ++i) { cout << col1[i]; } cout << "\n";*/ } int sum1 = countCols(col1, n), sum2 = countCols(col2, n); if(col1 == col2) { cout << "TAK\n"; return; } if(diff1 < diff2) { cout << "NIE\n"; } else { if(diff1 > diff2) { cout << "TAK\n"; } else { cout << (diff1 <= 1 || sum1 == sum2 ? "NIE" : "TAK") << "\n"; } } } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while(t--) { solve(); } } |