///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " using namespace std; typedef long long LL; int n; vector<vector<int>> adj; vector<array<int, 2>> edges; vector<bool> dead; int dest_mask; inline int color(int mask, int x) { return (mask & (1<<x))? 1 : 0; } string show(int mask) { string ret; for (int i = 0; i < n; ++i) { ret += (mask%2? "1" : "0"); mask /= 2; } return ret; } int depth = 0; bool seek(int mask) { DEBUG{ for (int i = 0; i < depth; ++i) { cout << " "; } } DEBUG COUT << show(mask) << endl; DEBUG depth++; if(mask == dest_mask) { return true; } dead[mask] = true; for (auto& edge : edges) { int i = edge[0]; int j = edge[1]; if(color(mask, i) != color(mask, j)) { int new_mask = mask ^ (1<<i); for (int k = 0; k < 2; ++k) { if (!dead[new_mask]) { if (seek(new_mask)) { return true; } } new_mask = mask ^ (1 << j); } } } return false; } int str2mask(string str) { int ret = 0; for (int i = 0; i < str.size(); ++i) { if(str[i] == '1') { ret += 1<<i; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int z; cin >> z; while(z--) { cin >> n; string start, dest; cin >> start >> dest; int start_mask = str2mask(start); dest_mask = str2mask(dest); DEBUG COUT << VAR(show(dest_mask)) << ENDL; adj.clear(); adj.resize(n); dead.resize(1<<n); for (int i = 0; i < dead.size(); ++i) { dead[i] = false; } edges.resize(n-1); for (int i = 0; i < n - 1; ++i) { int a,b ; cin >> a >> b; a--, b--; edges[i] = {a, b}; // adj[a].push_back(b); // adj[b].push_back(a); } cout << (seek(start_mask)? "TAK\n" : "NIE\n"); } }
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 | ///MEMENTO MEMO, MEMENTO LONG LONG #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " using namespace std; typedef long long LL; int n; vector<vector<int>> adj; vector<array<int, 2>> edges; vector<bool> dead; int dest_mask; inline int color(int mask, int x) { return (mask & (1<<x))? 1 : 0; } string show(int mask) { string ret; for (int i = 0; i < n; ++i) { ret += (mask%2? "1" : "0"); mask /= 2; } return ret; } int depth = 0; bool seek(int mask) { DEBUG{ for (int i = 0; i < depth; ++i) { cout << " "; } } DEBUG COUT << show(mask) << endl; DEBUG depth++; if(mask == dest_mask) { return true; } dead[mask] = true; for (auto& edge : edges) { int i = edge[0]; int j = edge[1]; if(color(mask, i) != color(mask, j)) { int new_mask = mask ^ (1<<i); for (int k = 0; k < 2; ++k) { if (!dead[new_mask]) { if (seek(new_mask)) { return true; } } new_mask = mask ^ (1 << j); } } } return false; } int str2mask(string str) { int ret = 0; for (int i = 0; i < str.size(); ++i) { if(str[i] == '1') { ret += 1<<i; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int z; cin >> z; while(z--) { cin >> n; string start, dest; cin >> start >> dest; int start_mask = str2mask(start); dest_mask = str2mask(dest); DEBUG COUT << VAR(show(dest_mask)) << ENDL; adj.clear(); adj.resize(n); dead.resize(1<<n); for (int i = 0; i < dead.size(); ++i) { dead[i] = false; } edges.resize(n-1); for (int i = 0; i < n - 1; ++i) { int a,b ; cin >> a >> b; a--, b--; edges[i] = {a, b}; // adj[a].push_back(b); // adj[b].push_back(a); } cout << (seek(start_mask)? "TAK\n" : "NIE\n"); } } |